Skip to content

Commit

Permalink
APIGOV-28137 - fix finding instance to update (#804)
Browse files Browse the repository at this point in the history
* fix finding instance to update

* fix test
  • Loading branch information
jcollins-axway authored Jun 26, 2024
1 parent e9f8e7a commit f8aabac
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 20 deletions.
12 changes: 10 additions & 2 deletions pkg/apic/apiservice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,11 @@ func TestUpdateService(t *testing.T) {
RespCode: http.StatusOK,
},
{
FileName: "./testdata/servicerevision.json", // for call to update the serviceRevision
FileName: "./testdata/servicerevision.json", // for call to get the serviceRevision count
RespCode: http.StatusOK,
},
{
FileName: "./testdata/servicerevision.json", // for call to get the serviceRevision count based on name
RespCode: http.StatusOK,
},
{
Expand Down Expand Up @@ -669,7 +673,11 @@ func TestUnstructuredConsumerInstanceData(t *testing.T) {
RespCode: http.StatusOK,
},
{
FileName: "./testdata/servicerevision.json", // this for call to create the serviceRevision
FileName: "./testdata/servicerevision.json", // this for call to get the serviceRevision count
RespCode: http.StatusOK,
},
{
FileName: "./testdata/servicerevision.json", // this for call to check if a specific revision name exists
RespCode: http.StatusOK,
},
{
Expand Down
41 changes: 23 additions & 18 deletions pkg/apic/apiserviceinstance.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import (
"encoding/json"
"errors"
"net/http"
"strconv"

"github.com/Axway/agent-sdk/pkg/util"

coreapi "github.com/Axway/agent-sdk/pkg/api"
apiv1 "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/api/v1"
management "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/management/v1alpha1"
defs "github.com/Axway/agent-sdk/pkg/apic/definitions"
utilerrors "github.com/Axway/agent-sdk/pkg/util/errors"
"github.com/Axway/agent-sdk/pkg/util/log"
)
Expand Down Expand Up @@ -185,7 +185,7 @@ func (c *ServiceClient) processInstance(serviceBody *ServiceBody) error {
instance := c.buildAPIServiceInstance(serviceBody, getRevisionPrefix(serviceBody), endpoints)

if serviceBody.serviceContext.serviceAction == updateAPI {
prevInst, err := c.getLastInstance(serviceBody, c.createAPIServerURL(instance.GetKindLink()))
prevInst, err := c.getInstance(serviceBody, c.createAPIServerURL(instance.GetKindLink()))
if err != nil {
return err
}
Expand Down Expand Up @@ -256,27 +256,32 @@ func createInstanceEndpoint(endpoints []EndpointDefinition) ([]management.ApiSer
return endPoints, nil
}

func (c *ServiceClient) getLastInstance(serviceBody *ServiceBody, url string) (*management.APIServiceInstance, error) {
// start from latest revision, find first instance
for i := serviceBody.serviceContext.revisionCount; i > 0; i-- {
revName := getRevisionPrefix(serviceBody)
if i > 1 {
revName += "." + strconv.Itoa(i)
}
queryParams := map[string]string{
"query": "metadata.references.name==" + revName,
}
func (c *ServiceClient) getInstance(serviceBody *ServiceBody, url string) (*management.APIServiceInstance, error) {
queryParams := map[string]string{
"query": "metadata.references.name==" + serviceBody.serviceContext.revisionName,
}
instances, err := c.GetAPIServiceInstances(queryParams, url)
if err != nil {
return nil, err
}
if len(instances) == 1 {
// return only instance
return instances[0], nil
}

instances, err := c.GetAPIServiceInstances(queryParams, url)
// check the instance for the stage agent details
for _, i := range instances {
stage, err := util.GetAgentDetailsValue(i, defs.AttrExternalAPIStage)
if err != nil {
return nil, err
continue
}

if len(instances) > 0 {
return instances[0], nil
if stage == serviceBody.Stage {
// found the stage match
return i, nil
}
c.logger.Debug("no instances were returned")
}

// if no instance found
return nil, nil
}

Expand Down

0 comments on commit f8aabac

Please sign in to comment.