Skip to content
1 change: 1 addition & 0 deletions cns/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ type NmAgentSupportedApisResponse struct {
type HomeAzResponse struct {
IsSupported bool `json:"isSupported"`
HomeAz uint `json:"homeAz"`
APIVersion uint `json:"apiVersion"`
}

type GetHomeAzResponse struct {
Expand Down
10 changes: 9 additions & 1 deletion cns/restserver/homeazmonitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,15 @@ 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, APIVersion is a uint, so its value >=0
// 0 should be valid when NMA version is old and does not have the apiVersion value in home az response
if azResponse.APIVersion > 0 && azResponse.APIVersion != 2 {
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, APIVersion: azResponse.APIVersion})
}

// 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), APIVersion: uint(2)},
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
3 changes: 2 additions & 1 deletion nmagent/responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ type NCVersionList struct {
}

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

type NodeIP struct {
Expand Down
Loading