Skip to content
5 changes: 3 additions & 2 deletions cns/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,8 +354,9 @@ type NmAgentSupportedApisResponse struct {
}

type HomeAzResponse struct {
IsSupported bool `json:"isSupported"`
HomeAz uint `json:"homeAz"`
IsSupported bool `json:"isSupported"`
HomeAz uint `json:"homeAz"`
NmaAppliedTheIPV6Fix bool `json:"NmaAppliedTheIPV6Fix"`
}

type GetHomeAzResponse struct {
Expand Down
9 changes: 8 additions & 1 deletion cns/restserver/homeazmonitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,14 @@ func (h *HomeAzMonitor) Populate(ctx context.Context) {
h.update(returnCode, returnMessage, cns.HomeAzResponse{IsSupported: true})
return
}
h.update(types.Success, "Get Home Az succeeded", cns.HomeAzResponse{IsSupported: true, HomeAz: azResponse.HomeAz})
// validate APIVersion value
if !azResponse.Valid() {
returnMessage := fmt.Sprintf("[HomeAzMonitor] invalid APIVersion value from nmagent: %d", azResponse.APIVersion)
returnCode := types.UnexpectedError
h.update(returnCode, returnMessage, cns.HomeAzResponse{IsSupported: true})
return
}
h.update(types.Success, "Get Home Az succeeded", cns.HomeAzResponse{IsSupported: true, HomeAz: azResponse.HomeAz, NmaAppliedTheIPV6Fix: azResponse.NmaAppliedTheIPV6Fix()})
}

// update constructs a GetHomeAzResponse entity and update its cache
Expand Down
33 changes: 23 additions & 10 deletions cns/restserver/homeazmonitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,23 @@ func TestHomeAzMonitor(t *testing.T) {
{
"happy path",
&fakes.NMAgentClientFake{
SupportedAPIsF: func(ctx context.Context) ([]string, error) {
SupportedAPIsF: func(_ context.Context) ([]string, error) {
return []string{"GetHomeAz"}, nil
},
GetHomeAzF: func(ctx context.Context) (nmagent.AzResponse, error) {
return nmagent.AzResponse{HomeAz: uint(1)}, nil
GetHomeAzF: func(_ context.Context) (nmagent.AzResponse, error) {
return nmagent.AzResponse{HomeAz: uint(1), APIVersion: uint(2)}, nil
},
},
cns.HomeAzResponse{IsSupported: true, HomeAz: uint(1)},
cns.HomeAzResponse{IsSupported: true, HomeAz: uint(1), NmaAppliedTheIPV6Fix: true},
false,
},
{
"getHomeAz is not supported in nmagent",
&fakes.NMAgentClientFake{
SupportedAPIsF: func(ctx context.Context) ([]string, error) {
SupportedAPIsF: func(_ context.Context) ([]string, error) {
return []string{"dummy"}, nil
},
GetHomeAzF: func(ctx context.Context) (nmagent.AzResponse, error) {
GetHomeAzF: func(_ context.Context) (nmagent.AzResponse, error) {
return nmagent.AzResponse{}, nil
},
},
Expand All @@ -50,23 +50,36 @@ func TestHomeAzMonitor(t *testing.T) {
{
"api supported but home az value is not valid",
&fakes.NMAgentClientFake{
SupportedAPIsF: func(ctx context.Context) ([]string, error) {
SupportedAPIsF: func(_ context.Context) ([]string, error) {
return []string{GetHomeAzAPIName}, nil
},
GetHomeAzF: func(ctx context.Context) (nmagent.AzResponse, error) {
GetHomeAzF: func(_ context.Context) (nmagent.AzResponse, error) {
return nmagent.AzResponse{HomeAz: 0}, nil
},
},
cns.HomeAzResponse{IsSupported: true},
true,
},
{
"api supported but apiVersion value is not valid",
&fakes.NMAgentClientFake{
SupportedAPIsF: func(_ context.Context) ([]string, error) {
return []string{GetHomeAzAPIName}, nil
},
GetHomeAzF: func(_ context.Context) (nmagent.AzResponse, error) {
return nmagent.AzResponse{HomeAz: uint(1), APIVersion: uint(3)}, nil
},
},
cns.HomeAzResponse{IsSupported: true},
true,
},
{
"api supported but got unexpected errors",
&fakes.NMAgentClientFake{
SupportedAPIsF: func(ctx context.Context) ([]string, error) {
SupportedAPIsF: func(_ context.Context) ([]string, error) {
return []string{GetHomeAzAPIName}, nil
},
GetHomeAzF: func(ctx context.Context) (nmagent.AzResponse, error) {
GetHomeAzF: func(_ context.Context) (nmagent.AzResponse, error) {
return nmagent.AzResponse{}, errors.New("unexpected error")
},
},
Expand Down
12 changes: 12 additions & 0 deletions nmagent/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,18 @@ func TestGetHomeAz(t *testing.T) {
map[string]interface{}{
"httpStatusCode": "200",
"HomeAz": 1,
"APIVersion": 0,
},
false,
},
{
"happy path with new version",
nmagent.AzResponse{HomeAz: uint(1), APIVersion: uint(2)},
"/machine/plugins?comp=nmagent&type=GetHomeAz%2Fapi-version%2F1",
map[string]interface{}{
"httpStatusCode": "200",
"HomeAz": 1,
"APIVersion": 2,
},
false,
},
Expand Down
14 changes: 13 additions & 1 deletion nmagent/responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,19 @@ type NCVersionList struct {
}

type AzResponse struct {
HomeAz uint `json:"homeAz"`
HomeAz uint `json:"homeAz"`
APIVersion uint `json:"apiVersion"`
}

func (az AzResponse) Valid() bool {
// 0 should be valid when NMA version is old and does not have the apiVersion value in home az response
//nolint:gomnd // these magic numbers are made by nma design
return az.APIVersion == 0 || az.APIVersion == 2
}

func (az AzResponse) NmaAppliedTheIPV6Fix() bool {
//nolint:gomnd // this magic number is made by nma design
return az.APIVersion == 2
}

type NodeIP struct {
Expand Down
Loading