2020
2121'use strict' ;
2222
23+ var nodeutil = require ( 'util' ) ;
24+
25+ /**
26+ * @type {module:common/serviceObject }
27+ * @private
28+ */
29+ var ServiceObject = require ( '../common/service-object.js' ) ;
30+
2331/**
2432 * @type {module:common/util }
2533 * @private
@@ -54,11 +62,88 @@ var util = require('../common/util.js');
5462 * var address = region.address('address1');
5563 */
5664function Address ( region , name ) {
65+ ServiceObject . call ( this , {
66+ parent : region ,
67+ baseUrl : '/addresses' ,
68+ id : name ,
69+ createMethod : region . createAddress . bind ( region ) ,
70+ exclude : [ 'setMetadata' ]
71+ } ) ;
72+
5773 this . region = region ;
58- this . name = name ;
59- this . metadata = { } ;
6074}
6175
76+ nodeutil . inherits ( Address , ServiceObject ) ;
77+
78+ /**
79+ * Create an address.
80+ *
81+ * @resource [Instances and Networks]{@link https://cloud.google.com/compute/docs/instances-and-network}
82+ * @resource [Address Resource]{@link https://cloud.google.com/compute/docs/reference/v1/addresses}
83+ * @resource [Addresses: insert API Documentation]{@link https://cloud.google.com/compute/docs/reference/v1/addresses/insert}
84+ *
85+ * @param {object= } options - See an
86+ * [Address resource](https://cloud.google.com/compute/docs/reference/v1/addresses).
87+ * @param {function } callback - The callback function.
88+ * @param {?error } callback.err - An error returned while making this request.
89+ * @param {module:compute/address } callback.address - The created Address
90+ * object.
91+ * @param {module:compute/operation } callback.operation - An operation object
92+ * that can be used to check the status of the request.
93+ * @param {object } callback.apiResponse - The full API response.
94+ *
95+ * @example
96+ * address.create(function(err, address, operation, apiResponse) {
97+ * // `address` is an Address object.
98+ *
99+ * // `operation` is an Operation object that can be used to check the status
100+ * // of the request.
101+ * });
102+ */
103+ Address . prototype . create = function ( ) {
104+ ServiceObject . prototype . create . apply ( this , arguments ) ;
105+ } ;
106+
107+ /**
108+ * Get an address if it exists. Also see {module:compute/address#getOrCreate}.
109+ *
110+ * @resource [Address Resource]{@link https://cloud.google.com/compute/docs/reference/v1/addresses}
111+ *
112+ * @param {function } callback - The callback function.
113+ * @param {?error } callback.err - An error returned while making this request.
114+ * @param {module:compute/address } callback.address - The Address object.
115+ * @param {object } callback.apiResponse - The full API response.
116+ *
117+ * @example
118+ * address.get(function(err, address, apiResponse) {
119+ * // `address` is an Address object.
120+ * });
121+ */
122+ Address . prototype . get = function ( ) {
123+ ServiceObject . prototype . get . apply ( this , arguments ) ;
124+ } ;
125+
126+ /**
127+ * Get an address if it exists, otherwise create one.
128+ *
129+ * @resource [Instances and Networks]{@link https://cloud.google.com/compute/docs/instances-and-network}
130+ * @resource [Address Resource]{@link https://cloud.google.com/compute/docs/reference/v1/addresses}
131+ * @resource [Addresses: insert API Documentation]{@link https://cloud.google.com/compute/docs/reference/v1/addresses/insert}
132+ *
133+ * @param {function } callback - The callback function.
134+ * @param {?error } callback.err - An error returned while making this request.
135+ * @param {module:compute/address } callback.address - The Address object.
136+ * @param {object } callback.apiResponse - The full API response.
137+ *
138+ * @example
139+ * address.getOrCreate(function(err, address, apiResponse) {
140+ * // `address` is an Address object.
141+ * });
142+ */
143+ Address . prototype . getOrCreate = function ( ) {
144+ ServiceObject . prototype . getOrCreate . apply ( this , arguments ) ;
145+ } ;
146+
62147/**
63148 * Delete the address.
64149 *
@@ -81,7 +166,10 @@ Address.prototype.delete = function(callback) {
81166
82167 var region = this . region ;
83168
84- this . makeReq_ ( 'DELETE' , '' , null , null , function ( err , resp ) {
169+ this . request ( {
170+ method : 'DELETE' ,
171+ uri : ''
172+ } , function ( err , resp ) {
85173 if ( err ) {
86174 callback ( err , null , resp ) ;
87175 return ;
@@ -108,38 +196,8 @@ Address.prototype.delete = function(callback) {
108196 * @example
109197 * address.getMetadata(function(err, metadata, apiResponse) {});
110198 */
111- Address . prototype . getMetadata = function ( callback ) {
112- callback = callback || util . noop ;
113-
114- var self = this ;
115-
116- this . makeReq_ ( 'GET' , '' , null , null , function ( err , resp ) {
117- if ( err ) {
118- callback ( err , null , resp ) ;
119- return ;
120- }
121-
122- self . metadata = resp ;
123-
124- callback ( null , self . metadata , resp ) ;
125- } ) ;
126- } ;
127-
128- /**
129- * Make a new request object from the provided arguments and wrap the callback
130- * to intercept non-successful responses.
131- *
132- * @private
133- *
134- * @param {string } method - Action.
135- * @param {string } path - Request path.
136- * @param {* } query - Request query object.
137- * @param {* } body - Request body contents.
138- * @param {function } callback - The callback function.
139- */
140- Address . prototype . makeReq_ = function ( method , path , query , body , callback ) {
141- path = '/addresses/' + this . name + path ;
142- this . region . makeReq_ ( method , path , query , body , callback ) ;
199+ Address . prototype . getMetadata = function ( ) {
200+ ServiceObject . prototype . getMetadata . apply ( this , arguments ) ;
143201} ;
144202
145203module . exports = Address ;
0 commit comments