@@ -45,17 +45,17 @@ func RetryAttempts(attempts uint) ClientOption {
4545}
4646
4747const (
48- vmUniqueIDProperty = "vmId"
49- imdsComputePath = "/metadata/instance/compute"
50- imdsNetworkPath = "/metadata/instance/network"
51- imdsVersionsPath = "/metadata/versions"
52- imdsDefaultAPIVersion = "api-version=2021-01-01"
53- imdsNCDetailsVersion = "api-version=2025-07-24"
54- imdsFormatJSON = "format=json"
55- metadataHeaderKey = "Metadata"
56- metadataHeaderValue = "true"
57- defaultRetryAttempts = 3
58- defaultIMDSEndpoint = "http://169.254.169.254"
48+ vmUniqueIDProperty = "vmId"
49+ imdsComputePath = "/metadata/instance/compute"
50+ imdsNetworkPath = "/metadata/instance/network"
51+ imdsVersionsPath = "/metadata/versions"
52+ imdsDefaultAPIVersion = "api-version=2021-01-01"
53+ imdsNCDetailsVersion = "api-version=2025-07-24"
54+ imdsFormatJSON = "format=json"
55+ metadataHeaderKey = "Metadata"
56+ metadataHeaderValue = "true"
57+ defaultRetryAttempts = 3
58+ defaultIMDSEndpoint = "http://169.254.169.254"
5959)
6060
6161var (
@@ -132,7 +132,6 @@ func (c *Client) GetNetworkInterfaces(ctx context.Context) ([]NetworkInterface,
132132 return networkData .Interface , nil
133133}
134134
135-
136135func (c * Client ) getInstanceMetadata (ctx context.Context , imdsMetadataPath , imdsAPIVersion string ) (map [string ]any , error ) {
137136 imdsRequestURL , err := url .JoinPath (c .config .endpoint , imdsMetadataPath )
138137 if err != nil {
@@ -166,82 +165,85 @@ func (c *Client) getInstanceMetadata(ctx context.Context, imdsMetadataPath, imds
166165}
167166
168167func (c * Client ) GetIMDSVersions (ctx context.Context ) (* APIVersionsResponse , error ) {
169- var versionsResp APIVersionsResponse
170- err := retry .Do (func () error {
171- // Build the URL for the versions endpoint
172- imdsRequestURL , err := url .JoinPath (c .config .endpoint , imdsVersionsPath )
173- if err != nil {
174- return errors .Wrap (err , "unable to build path to IMDS versions endpoint" )
175- }
176-
177- req , err := http .NewRequestWithContext (ctx , http .MethodGet , imdsRequestURL , http .NoBody )
178- if err != nil {
179- return errors .Wrap (err , "error building IMDS versions http request" )
180- }
181-
182- req .Header .Add (metadataHeaderKey , metadataHeaderValue )
183- resp , err := c .cli .Do (req )
184- if err != nil {
185- return errors .Wrap (err , "error querying IMDS versions API" )
186- }
187- defer resp .Body .Close ()
188-
189- if resp .StatusCode != http .StatusOK {
190- return errors .Wrapf (ErrUnexpectedStatusCode , "unexpected status code %d" , resp .StatusCode )
191- }
192-
193- if err := json .NewDecoder (resp .Body ).Decode (& versionsResp ); err != nil {
194- return errors .Wrap (err , "error decoding IMDS versions response as json" )
195- }
196-
197- return nil
198- }, retry .Context (ctx ), retry .Attempts (c .config .retryAttempts ), retry .DelayType (retry .BackOffDelay ))
199-
200- if err != nil {
201- return nil , errors .Wrap (err , "exhausted retries querying IMDS versions" )
202- }
203-
204- return & versionsResp , nil
168+ var versionsResp APIVersionsResponse
169+ err := retry .Do (func () error {
170+ // Build the URL for the versions endpoint
171+ imdsRequestURL , err := url .JoinPath (c .config .endpoint , imdsVersionsPath )
172+ if err != nil {
173+ return errors .Wrap (err , "unable to build path to IMDS versions endpoint" )
174+ }
175+
176+ req , err := http .NewRequestWithContext (ctx , http .MethodGet , imdsRequestURL , http .NoBody )
177+ if err != nil {
178+ return errors .Wrap (err , "error building IMDS versions http request" )
179+ }
180+
181+ req .Header .Add (metadataHeaderKey , metadataHeaderValue )
182+ resp , err := c .cli .Do (req )
183+ if err != nil {
184+ return errors .Wrap (err , "error querying IMDS versions API" )
185+ }
186+ defer resp .Body .Close ()
187+
188+ if resp .StatusCode != http .StatusOK {
189+ return errors .Wrapf (ErrUnexpectedStatusCode , "unexpected status code %d" , resp .StatusCode )
190+ }
191+
192+ if err := json .NewDecoder (resp .Body ).Decode (& versionsResp ); err != nil {
193+ return errors .Wrap (err , "error decoding IMDS versions response as json" )
194+ }
195+
196+ return nil
197+ }, retry .Context (ctx ), retry .Attempts (c .config .retryAttempts ), retry .DelayType (retry .BackOffDelay ))
198+
199+ if err != nil {
200+ return nil , errors .Wrap (err , "exhausted retries querying IMDS versions" )
201+ }
202+
203+ return & versionsResp , nil
205204}
206205
207206// Required for marshaling/unmarshaling of mac address
208207type HardwareAddr net.HardwareAddr
209208
210- func (h HardwareAddr ) MarshalJSON () ([]byte , error ) {
211- return json .Marshal (net .HardwareAddr (h ).String ())
209+ func (h * HardwareAddr ) MarshalJSON () ([]byte , error ) {
210+ data , err := json .Marshal (net .HardwareAddr (* h ).String ())
211+ if err != nil {
212+ return nil , errors .Wrap (err , "failed to marshal hardware address" )
213+ }
214+ return data , nil
212215}
213216
214217func (h * HardwareAddr ) UnmarshalJSON (data []byte ) error {
215- var s string
216- if err := json .Unmarshal (data , & s ); err != nil {
217- return err
218- }
219- mac , err := net .ParseMAC (s )
220- if err != nil {
221- return err
222- }
223- * h = HardwareAddr (mac )
224- return nil
218+ var s string
219+ if err := json .Unmarshal (data , & s ); err != nil {
220+ return errors . Wrap ( err , "failed to unmarshal JSON data" )
221+ }
222+ mac , err := net .ParseMAC (s )
223+ if err != nil {
224+ return errors . Wrap ( err , "failed to parse MAC address" )
225+ }
226+ * h = HardwareAddr (mac )
227+ return nil
225228}
226229
227- func (h HardwareAddr ) String () string {
228- return net .HardwareAddr (h ).String ()
230+ func (h * HardwareAddr ) String () string {
231+ return net .HardwareAddr (* h ).String ()
229232}
230233
231234// NetworkInterface represents a network interface from IMDS
232235type NetworkInterface struct {
233236 // IMDS returns compartment fields - these are mapped to NC ID and NC version
234- MacAddress HardwareAddr `json:"macAddress"`
235- InterfaceCompartmentID string `json:"interfaceCompartmentID,omitempty"`
237+ MacAddress HardwareAddr `json:"macAddress"`
238+ InterfaceCompartmentID string `json:"interfaceCompartmentID,omitempty"`
236239}
237240
238241// NetworkInterfaces represents the network interfaces from IMDS
239242type NetworkInterfaces struct {
240243 Interface []NetworkInterface `json:"interface"`
241244}
242245
243-
244246// APIVersionsResponse represents versions form IMDS
245247type APIVersionsResponse struct {
246- APIVersions []string `json:"apiVersions"`
247- }
248+ APIVersions []string `json:"apiVersions"`
249+ }
0 commit comments