@@ -19,15 +19,34 @@ const (
1919 defaultCnsURL = "http://localhost:10090"
2020)
2121
22- // NewCnsClient create a new cns client.
23- func NewCnsClient (url string ) (* CNSClient , error ) {
24- if url == "" {
25- url = defaultCnsURL
22+ var (
23+ cnsClient * CNSClient
24+ )
25+
26+ // InitCnsClient initializes new cns client and returns the object
27+ func InitCnsClient (url string ) (* CNSClient , error ) {
28+ if cnsClient == nil {
29+ if url == "" {
30+ url = defaultCnsURL
31+ }
32+
33+ cnsClient = & CNSClient {
34+ connectionURL : url ,
35+ }
2636 }
2737
28- return & CNSClient {
29- connectionURL : url ,
30- }, nil
38+ return cnsClient , nil
39+ }
40+
41+ // GetCnsClient returns the cns client object
42+ func GetCnsClient () (* CNSClient , error ) {
43+ var err error
44+
45+ if cnsClient == nil {
46+ err = fmt .Errorf ("[Azure CNSClient] CNS Client not initialized" )
47+ }
48+
49+ return cnsClient , err
3150}
3251
3352// GetNetworkConfiguration Request to get network config.
@@ -77,3 +96,105 @@ func (cnsClient *CNSClient) GetNetworkConfiguration(orchestratorContext []byte)
7796
7897 return & resp , nil
7998}
99+
100+ // CreateHostNCApipaEndpoint creates an endpoint in APIPA network for host container connectivity.
101+ func (cnsClient * CNSClient ) CreateHostNCApipaEndpoint (
102+ networkContainerID string ) (string , error ) {
103+ var (
104+ err error
105+ body bytes.Buffer
106+ )
107+
108+ httpc := & http.Client {}
109+ url := cnsClient .connectionURL + cns .CreateHostNCApipaEndpointPath
110+ log .Printf ("CreateHostNCApipaEndpoint url: %v for NC: %s" , url , networkContainerID )
111+
112+ payload := & cns.CreateHostNCApipaEndpointRequest {
113+ NetworkContainerID : networkContainerID ,
114+ }
115+
116+ if err = json .NewEncoder (& body ).Encode (payload ); err != nil {
117+ log .Errorf ("encoding json failed with %v" , err )
118+ return "" , err
119+ }
120+
121+ res , err := httpc .Post (url , "application/json" , & body )
122+ if err != nil {
123+ log .Errorf ("[Azure CNSClient] HTTP Post returned error %v" , err .Error ())
124+ return "" , err
125+ }
126+
127+ defer res .Body .Close ()
128+
129+ if res .StatusCode != http .StatusOK {
130+ errMsg := fmt .Sprintf ("[Azure CNSClient] CreateHostNCApipaEndpoint: Invalid http status code: %v" ,
131+ res .StatusCode )
132+ log .Errorf (errMsg )
133+ return "" , fmt .Errorf (errMsg )
134+ }
135+
136+ var resp cns.CreateHostNCApipaEndpointResponse
137+
138+ if err = json .NewDecoder (res .Body ).Decode (& resp ); err != nil {
139+ log .Errorf ("[Azure CNSClient] Error parsing CreateHostNCApipaEndpoint response resp: %v err: %v" ,
140+ res .Body , err .Error ())
141+ return "" , err
142+ }
143+
144+ if resp .Response .ReturnCode != 0 {
145+ log .Errorf ("[Azure CNSClient] CreateHostNCApipaEndpoint received error response :%v" , resp .Response .Message )
146+ return "" , fmt .Errorf (resp .Response .Message )
147+ }
148+
149+ return resp .EndpointID , nil
150+ }
151+
152+ // DeleteHostNCApipaEndpoint deletes the endpoint in APIPA network created for host container connectivity.
153+ func (cnsClient * CNSClient ) DeleteHostNCApipaEndpoint (networkContainerID string ) error {
154+ var body bytes.Buffer
155+
156+ httpc := & http.Client {}
157+ url := cnsClient .connectionURL + cns .DeleteHostNCApipaEndpointPath
158+ log .Printf ("DeleteHostNCApipaEndpoint url: %v for NC: %s" , url , networkContainerID )
159+
160+ payload := & cns.DeleteHostNCApipaEndpointRequest {
161+ NetworkContainerID : networkContainerID ,
162+ }
163+
164+ err := json .NewEncoder (& body ).Encode (payload )
165+ if err != nil {
166+ log .Errorf ("encoding json failed with %v" , err )
167+ return err
168+ }
169+
170+ res , err := httpc .Post (url , "application/json" , & body )
171+ if err != nil {
172+ log .Errorf ("[Azure CNSClient] HTTP Post returned error %v" , err .Error ())
173+ return err
174+ }
175+
176+ defer res .Body .Close ()
177+
178+ if res .StatusCode != http .StatusOK {
179+ errMsg := fmt .Sprintf ("[Azure CNSClient] DeleteHostNCApipaEndpoint: Invalid http status code: %v" ,
180+ res .StatusCode )
181+ log .Errorf (errMsg )
182+ return fmt .Errorf (errMsg )
183+ }
184+
185+ var resp cns.DeleteHostNCApipaEndpointResponse
186+
187+ err = json .NewDecoder (res .Body ).Decode (& resp )
188+ if err != nil {
189+ log .Errorf ("[Azure CNSClient] Error parsing DeleteHostNCApipaEndpoint response resp: %v err: %v" ,
190+ res .Body , err .Error ())
191+ return err
192+ }
193+
194+ if resp .Response .ReturnCode != 0 {
195+ log .Errorf ("[Azure CNSClient] DeleteHostNCApipaEndpoint received error response :%v" , resp .Response .Message )
196+ return fmt .Errorf (resp .Response .Message )
197+ }
198+
199+ return nil
200+ }
0 commit comments