Skip to content

Commit 21b6fb4

Browse files
fix formatting errors
1 parent 94e969b commit 21b6fb4

File tree

5 files changed

+476
-470
lines changed

5 files changed

+476
-470
lines changed

cns/fakes/imdsclientfake.go

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -60,38 +60,38 @@ func (m *MockIMDSClient) GetVMUniqueID(ctx context.Context) (string, error) {
6060
}
6161

6262
func (m *MockIMDSClient) GetNetworkInterfaces(ctx context.Context) ([]imds.NetworkInterface, error) {
63-
if ctx.Value(SimulateError) != nil {
64-
return nil, imds.ErrUnexpectedStatusCode
65-
}
63+
if ctx.Value(SimulateError) != nil {
64+
return nil, imds.ErrUnexpectedStatusCode
65+
}
6666

67-
// Parse MAC addresses for testing
68-
macAddr1, _ := net.ParseMAC("00:15:5d:01:02:01")
69-
macAddr2, _ := net.ParseMAC("00:15:5d:01:02:02")
67+
// Parse MAC addresses for testing
68+
macAddr1, _ := net.ParseMAC("00:15:5d:01:02:01")
69+
macAddr2, _ := net.ParseMAC("00:15:5d:01:02:02")
7070

71-
// Return some mock network interfaces for testing
72-
return []imds.NetworkInterface{
73-
{
74-
InterfaceCompartmentID: "nc1",
75-
MacAddress: imds.HardwareAddr(macAddr1),
76-
},
77-
{
78-
InterfaceCompartmentID: "nc2",
79-
MacAddress: imds.HardwareAddr(macAddr2),
80-
},
81-
}, nil
71+
// Return some mock network interfaces for testing
72+
return []imds.NetworkInterface{
73+
{
74+
InterfaceCompartmentID: "nc1",
75+
MacAddress: imds.HardwareAddr(macAddr1),
76+
},
77+
{
78+
InterfaceCompartmentID: "nc2",
79+
MacAddress: imds.HardwareAddr(macAddr2),
80+
},
81+
}, nil
8282
}
8383

8484
func (m *MockIMDSClient) GetIMDSVersions(ctx context.Context) (*imds.APIVersionsResponse, error) {
85-
if ctx.Value(SimulateError) != nil {
86-
return nil, imds.ErrUnexpectedStatusCode
87-
}
85+
if ctx.Value(SimulateError) != nil {
86+
return nil, imds.ErrUnexpectedStatusCode
87+
}
8888

89-
// Return supported API versions including the expected one
90-
return &imds.APIVersionsResponse{
91-
APIVersions: []string{
92-
"2017-03-01",
93-
"2021-01-01",
94-
"2025-07-24",
95-
},
96-
}, nil
89+
// Return supported API versions including the expected one
90+
return &imds.APIVersionsResponse{
91+
APIVersions: []string{
92+
"2017-03-01",
93+
"2021-01-01",
94+
"2025-07-24",
95+
},
96+
}, nil
9797
}

cns/imds/client.go

Lines changed: 69 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,17 @@ func RetryAttempts(attempts uint) ClientOption {
4545
}
4646

4747
const (
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

6161
var (
@@ -132,7 +132,6 @@ func (c *Client) GetNetworkInterfaces(ctx context.Context) ([]NetworkInterface,
132132
return networkData.Interface, nil
133133
}
134134

135-
136135
func (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

168167
func (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
208207
type 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

214217
func (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
232235
type 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
239242
type NetworkInterfaces struct {
240243
Interface []NetworkInterface `json:"interface"`
241244
}
242245

243-
244246
// APIVersionsResponse represents versions form IMDS
245247
type APIVersionsResponse struct {
246-
APIVersions []string `json:"apiVersions"`
247-
}
248+
APIVersions []string `json:"apiVersions"`
249+
}

0 commit comments

Comments
 (0)