@@ -3,16 +3,21 @@ package nmagentclient
33import (
44 "bytes"
55 "encoding/json"
6+ "encoding/xml"
7+ "fmt"
68 "net/http"
79
810 "github.com/Azure/azure-container-networking/cns/logger"
911 "github.com/Azure/azure-container-networking/common"
1012)
1113
1214const (
15+ //GetNmAgentSupportedApiURLFmt Api endpoint to get supported Apis of NMAgent
16+ GetNmAgentSupportedApiURLFmt = "http://%s/machine/plugins/?comp=nmagent&type=GetSupportedApis"
1317 GetNetworkContainerVersionURLFmt = "http://%s/machine/plugins/?comp=nmagent&type=NetworkManagement/interfaces/%s/networkContainers/%s/version/authenticationToken/%s/api-version/1"
1418)
1519
20+ //WireServerIP - wire server ip
1621var WireserverIP = "168.63.129.16"
1722
1823// NMANetworkContainerResponse - NMAgent response.
@@ -22,6 +27,10 @@ type NMANetworkContainerResponse struct {
2227 Version string `json:"version"`
2328}
2429
30+ type NMAgentSupportedApisResponseXML struct {
31+ SupportedApis []string `xml:"type"`
32+ }
33+
2534// JoinNetwork joins the given network
2635func JoinNetwork (
2736 networkID string ,
@@ -88,3 +97,55 @@ func GetNetworkContainerVersion(
8897 networkContainerID , response , err )
8998 return response , err
9099}
100+
101+ // GetNmAgentSupportedApis :- Retrieves Supported Apis from NMAgent
102+ func GetNmAgentSupportedApis (
103+ httpc * http.Client ,
104+ getNmAgentSupportedApisURL string ) ([]string , error ) {
105+ var (
106+ returnErr error
107+ )
108+
109+ if getNmAgentSupportedApisURL == "" {
110+ getNmAgentSupportedApisURL = fmt .Sprintf (
111+ GetNmAgentSupportedApiURLFmt , WireserverIP )
112+ }
113+
114+ response , err := httpc .Get (getNmAgentSupportedApisURL )
115+ if err != nil {
116+ returnErr = fmt .Errorf (
117+ "Failed to retrieve Supported Apis from NMAgent with error %v" ,
118+ err .Error ())
119+ logger .Errorf ("[Azure-CNS] %s" , returnErr )
120+ return nil , returnErr
121+ }
122+ if response == nil {
123+ returnErr = fmt .Errorf (
124+ "Response from getNmAgentSupportedApis call is <nil>" )
125+ logger .Errorf ("[Azure-CNS] %s" , returnErr )
126+ return nil , returnErr
127+ }
128+ defer response .Body .Close ()
129+
130+ if response .StatusCode != http .StatusOK {
131+ returnErr = fmt .Errorf (
132+ "Failed to retrieve Supported Apis from NMAgent with StatusCode: %d" ,
133+ response .StatusCode )
134+ logger .Errorf ("[Azure-CNS] %s" , returnErr )
135+ return nil , returnErr
136+ }
137+
138+ var xmlDoc NMAgentSupportedApisResponseXML
139+ decoder := xml .NewDecoder (response .Body )
140+ err = decoder .Decode (& xmlDoc )
141+ if err != nil {
142+ returnErr = fmt .Errorf (
143+ "Failed to decode XML response of Supported Apis from NMAgent with error %v" ,
144+ err .Error ())
145+ logger .Errorf ("[Azure-CNS] %s" , returnErr )
146+ return nil , returnErr
147+ }
148+
149+ logger .Printf ("[NMAgentClient][Response] GetNmAgentSupportedApis. Response: %+v." , response )
150+ return xmlDoc .SupportedApis , nil
151+ }
0 commit comments