@@ -16,7 +16,8 @@ type CNSClient struct {
1616}
1717
1818const (
19- defaultCnsURL = "http://localhost:10090"
19+ defaultCnsURL = "http://localhost:10090"
20+ contentTypeJSON = "application/json"
2021)
2122
2223var (
@@ -67,7 +68,7 @@ func (cnsClient *CNSClient) GetNetworkConfiguration(orchestratorContext []byte)
6768 return nil , err
6869 }
6970
70- res , err := httpc .Post (url , "application/json" , & body )
71+ res , err := httpc .Post (url , contentTypeJSON , & body )
7172 if err != nil {
7273 log .Errorf ("[Azure CNSClient] HTTP Post returned error %v" , err .Error ())
7374 return nil , err
@@ -98,8 +99,7 @@ func (cnsClient *CNSClient) GetNetworkConfiguration(orchestratorContext []byte)
9899}
99100
100101// CreateHostNCApipaEndpoint creates an endpoint in APIPA network for host container connectivity.
101- func (cnsClient * CNSClient ) CreateHostNCApipaEndpoint (
102- networkContainerID string ) (string , error ) {
102+ func (cnsClient * CNSClient ) CreateHostNCApipaEndpoint (networkContainerID string ) (string , error ) {
103103 var (
104104 err error
105105 body bytes.Buffer
@@ -118,7 +118,7 @@ func (cnsClient *CNSClient) CreateHostNCApipaEndpoint(
118118 return "" , err
119119 }
120120
121- res , err := httpc .Post (url , "application/json" , & body )
121+ res , err := httpc .Post (url , contentTypeJSON , & body )
122122 if err != nil {
123123 log .Errorf ("[Azure CNSClient] HTTP Post returned error %v" , err .Error ())
124124 return "" , err
@@ -167,7 +167,7 @@ func (cnsClient *CNSClient) DeleteHostNCApipaEndpoint(networkContainerID string)
167167 return err
168168 }
169169
170- res , err := httpc .Post (url , "application/json" , & body )
170+ res , err := httpc .Post (url , contentTypeJSON , & body )
171171 if err != nil {
172172 log .Errorf ("[Azure CNSClient] HTTP Post returned error %v" , err .Error ())
173173 return err
@@ -198,3 +198,112 @@ func (cnsClient *CNSClient) DeleteHostNCApipaEndpoint(networkContainerID string)
198198
199199 return nil
200200}
201+
202+ // RequestIPAddress calls the requestIPAddress in CNS
203+ func (cnsClient * CNSClient ) RequestIPAddress (orchestratorContext []byte ) (* cns.GetIPConfigResponse , error ) {
204+ var (
205+ err error
206+ res * http.Response
207+ response * cns.GetIPConfigResponse
208+ )
209+
210+ defer func () {
211+ if err != nil {
212+ cnsClient .ReleaseIPAddress (orchestratorContext )
213+ }
214+ }()
215+
216+ var body bytes.Buffer
217+
218+ httpc := & http.Client {}
219+ url := cnsClient .connectionURL + cns .RequestIPConfig
220+
221+ payload := & cns.GetNetworkContainerRequest {
222+ OrchestratorContext : orchestratorContext ,
223+ }
224+
225+ err = json .NewEncoder (& body ).Encode (payload )
226+ if err != nil {
227+ log .Errorf ("encoding json failed with %v" , err )
228+ return response , err
229+ }
230+
231+ res , err = httpc .Post (url , contentTypeJSON , & body )
232+ if err != nil {
233+ log .Errorf ("[Azure CNSClient] HTTP Post returned error %v" , err .Error ())
234+ return response , err
235+ }
236+
237+ defer res .Body .Close ()
238+
239+ if res .StatusCode != http .StatusOK {
240+ errMsg := fmt .Sprintf ("[Azure CNSClient] RequestIPAddress invalid http status code: %v" , res .StatusCode )
241+ log .Errorf (errMsg )
242+ return response , fmt .Errorf (errMsg )
243+ }
244+
245+ err = json .NewDecoder (res .Body ).Decode (& response )
246+ if err != nil {
247+ log .Errorf ("[Azure CNSClient] Error received while parsing RequestIPAddress response resp:%v err:%v" , res .Body , err .Error ())
248+ return response , err
249+ }
250+
251+ if response .Response .ReturnCode != 0 {
252+ log .Errorf ("[Azure CNSClient] RequestIPAddress received error response :%v" , response .Response .Message )
253+ return response , fmt .Errorf (response .Response .Message )
254+ }
255+
256+ return response , err
257+ }
258+
259+ // ReleaseIPAddress calls releaseIPAddress on CNS
260+ func (cnsClient * CNSClient ) ReleaseIPAddress (orchestratorContext []byte ) error {
261+ var (
262+ err error
263+ res * http.Response
264+ body bytes.Buffer
265+ )
266+
267+ httpc := & http.Client {}
268+ url := cnsClient .connectionURL + cns .ReleaseIPConfig
269+ log .Printf ("ReleaseIPAddress url %v" , url )
270+
271+ payload := & cns.GetNetworkContainerRequest {
272+ OrchestratorContext : orchestratorContext ,
273+ }
274+
275+ err = json .NewEncoder (& body ).Encode (payload )
276+ if err != nil {
277+ log .Errorf ("encoding json failed with %v" , err )
278+ return err
279+ }
280+
281+ res , err = httpc .Post (url , contentTypeJSON , & body )
282+ if err != nil {
283+ log .Errorf ("[Azure CNSClient] HTTP Post returned error %v" , err .Error ())
284+ return err
285+ }
286+
287+ defer res .Body .Close ()
288+
289+ if res .StatusCode != http .StatusOK {
290+ errMsg := fmt .Sprintf ("[Azure CNSClient] ReleaseIPAddress invalid http status code: %v" , res .StatusCode )
291+ log .Errorf (errMsg )
292+ return fmt .Errorf (errMsg )
293+ }
294+
295+ var resp cns.Response
296+
297+ err = json .NewDecoder (res .Body ).Decode (& resp )
298+ if err != nil {
299+ log .Errorf ("[Azure CNSClient] Error received while parsing ReleaseIPAddress response resp:%v err:%v" , res .Body , err .Error ())
300+ return err
301+ }
302+
303+ if resp .ReturnCode != 0 {
304+ log .Errorf ("[Azure CNSClient] ReleaseIPAddress received error response :%v" , resp .Message )
305+ return fmt .Errorf (resp .Message )
306+ }
307+
308+ return err
309+ }
0 commit comments