Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

APIGOV-24064 - updates to add spec hash map to api service agent details #594

Merged
merged 5 commits into from
Nov 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 21 additions & 14 deletions pkg/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,10 @@ func (c *httpClient) Send(request Request) (*Response, error) {
return nil, err
}
reqID := uuid.New().String()

// Logging for the HTTP request
statusCode := 0
receivedData := int64(0)
defer func() {
duration := time.Since(startTime)
targetURL := req.URL.String()
Expand All @@ -318,23 +320,27 @@ func (c *httpClient) Send(request Request) (*Response, error) {
targetURL = req.URL.Scheme + "://" + entryHost + req.URL.Path
}
}

logger := c.logger.
WithField("id", reqID).
WithField("method", req.Method).
WithField("status", statusCode).
WithField("duration(ms)", duration.Milliseconds()).
WithField("url", targetURL)

if req.ContentLength > 0 {
logger = logger.WithField("sent(bytes)", req.ContentLength)
}

if receivedData > 0 {
logger = logger.WithField("received(bytes)", receivedData)
}

if err != nil {
c.logger.
WithField("id", reqID).
WithField("method", req.Method).
WithField("status", statusCode).
WithField("duration(ms)", duration.Milliseconds()).
WithField("url", targetURL).
WithError(err).
logger.WithError(err).
Trace("request failed")
} else {
c.logger.
WithField("id", reqID).
WithField("method", req.Method).
WithField("status", statusCode).
WithField("duration(ms)", duration.Milliseconds()).
WithField("url", targetURL).
Trace("request succeeded")
logger.Trace("request succeeded")
}
}()

Expand All @@ -356,6 +362,7 @@ func (c *httpClient) Send(request Request) (*Response, error) {
defer res.Body.Close()

statusCode = res.StatusCode
receivedData = res.ContentLength
parseResponse, err := c.prepareAPIResponse(res, timer)

return parseResponse, err
Expand Down
12 changes: 10 additions & 2 deletions pkg/apic/apiservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,15 @@ func (c *ServiceClient) updateAPIService(serviceBody *ServiceBody, svc *manageme
svc.Status = buildAPIServiceStatusSubResource(ownerErr)

svcDetails := buildAgentDetailsSubResource(serviceBody, true, serviceBody.ServiceAgentDetails)
sub := util.MergeMapStringInterface(util.GetAgentDetails(svc), svcDetails)
util.SetAgentDetails(svc, sub)
newSVCDetails := util.MergeMapStringInterface(util.GetAgentDetails(svc), svcDetails)
util.SetAgentDetails(svc, newSVCDetails)

// get the specHashes from the existing service
if revDetails, found := newSVCDetails[specHashes]; found {
if specHashes, ok := revDetails.(map[string]interface{}); ok {
serviceBody.specHashes = specHashes
}
}

if serviceBody.Image != "" {
svc.Spec.Icon = management.ApiServiceSpecIcon{
Expand Down Expand Up @@ -135,6 +142,7 @@ func (c *ServiceClient) processService(serviceBody *ServiceBody) (*management.AP
serviceURL := c.cfg.GetServicesURL()
httpMethod := http.MethodPost
serviceBody.serviceContext.serviceAction = addAPI
serviceBody.specHashes = map[string]interface{}{}

// If service exists, update existing service
svc, err := c.getAPIServiceFromCache(serviceBody)
Expand Down
83 changes: 31 additions & 52 deletions pkg/apic/apiservice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,10 @@ func TestUpdateService(t *testing.T) {
FileName: "./testdata/consumerinstance.json", // for call to update the consumerInstance subresource
RespCode: http.StatusOK,
},
{
FileName: "./testdata/apiservice.json", // for call to update the service subresource
RespCode: http.StatusOK,
},
})

cloneServiceBody := serviceBody
Expand Down Expand Up @@ -373,74 +377,42 @@ func TestUpdateService(t *testing.T) {
func Test_PublishServiceError(t *testing.T) {
client, httpClient := GetTestServiceClient()

// tests for updating existing revision
// this is a failure test
httpClient.SetResponses([]api.MockResponse{
{
FileName: "./testdata/apiservice-list.json", // for call to get the service
RespCode: http.StatusOK,
},
{
FileName: "./testdata/apiservice.json", // for call to update the service
RespCode: http.StatusOK,
},
{
FileName: "./testdata/agent-details-sr.json", // this for call to create the service
RespCode: http.StatusOK,
},
{
FileName: "./testdata/existingservicerevisions.json", // for call to get the serviceRevision
RespCode: http.StatusOK,
RespCode: http.StatusRequestTimeout,
},
})

apiSvc, err := client.PublishService(&serviceBody)
assert.NotNil(t, err)
assert.Nil(t, apiSvc)
}

func Test_processRevision(t *testing.T) {
client, httpClient := GetTestServiceClient()

// tests for updating existing revision
httpClient.SetResponses([]api.MockResponse{
{
FileName: "./testdata/servicerevision.json", // for call to update the serviceRevision
RespCode: http.StatusOK,
},
{
FileName: "./testdata/agent-details-sr.json", // this for call to create the service
FileName: "./testdata/servicerevision.json", // for call to update the serviceRevision x-agent-details
RespCode: http.StatusOK,
},
{
FileName: "./testdata/existingserviceinstances.json", // for call to get instance
RespCode: http.StatusOK,
},
{
FileName: "./testdata/serviceinstance.json", // for call to update the serviceInstance
FileName: "./testdata/servicerevision.json", // for call to update the serviceRevision
RespCode: http.StatusOK,
},
{
FileName: "./testdata/existingconsumerinstances.json", // for call to check existance of the consumerInstance
FileName: "./testdata/servicerevision.json", // for call to update the serviceRevision x-agent-details
RespCode: http.StatusOK,
},
{
FileName: "./testdata/consumerinstance.json", // for call to update the consumerInstance
RespCode: http.StatusOK,
},
{
FileName: "./testdata/agent-details-sr.json", // this for call to create the service
RespCode: http.StatusOK,
},
})
// this is a failure test
httpClient.SetResponses([]api.MockResponse{
{
RespCode: http.StatusRequestTimeout,
},
})

apiSvc, err := client.PublishService(&serviceBody)
assert.NotNil(t, err)
assert.Nil(t, apiSvc)
}

func Test_processRevision(t *testing.T) {
client, _ := GetTestServiceClient()
cloneServiceBody := serviceBody
// Alt Revision
cloneServiceBody.AltRevisionPrefix = "1.1.1"
client.processRevision(&cloneServiceBody)
assert.Contains(t, cloneServiceBody.serviceContext.revisionName, "1.1.1")
// Normal Revision
cloneServiceBody.AltRevisionPrefix = ""
client.processRevision(&cloneServiceBody)
assert.NotEqual(t, "", cloneServiceBody.serviceContext.revisionName)
}
Expand Down Expand Up @@ -647,6 +619,10 @@ func TestUnstructuredConsumerInstanceData(t *testing.T) {
FileName: "./testdata/agent-details-sr.json", // this for call to create the service
RespCode: http.StatusOK,
},
{
FileName: "./testdata/apiservice.json", // this for call to create the service
RespCode: http.StatusCreated,
},
})

// Test thrift object
Expand All @@ -672,7 +648,7 @@ func TestUnstructuredConsumerInstanceData(t *testing.T) {

// Get second to last request as consumerinstance
var consInst management.ConsumerInstance
err = json.Unmarshal(httpClient.Requests[len(httpClient.Requests)-2].Body, &consInst)
err = json.Unmarshal(httpClient.Requests[len(httpClient.Requests)-3].Body, &consInst)
assert.Nil(t, err)

// Only asset type set, label and asset type are equal
Expand Down Expand Up @@ -728,6 +704,10 @@ func TestUnstructuredConsumerInstanceData(t *testing.T) {
FileName: "./testdata/agent-details-sr.json", // this for call to create the service
RespCode: http.StatusOK,
},
{
FileName: "./testdata/apiservice.json", // this for call to create the service
RespCode: http.StatusCreated,
},
})

label := "Apache Thrift"
Expand All @@ -746,8 +726,7 @@ func TestUnstructuredConsumerInstanceData(t *testing.T) {

// Get last request as consumerinstance
consInst = management.ConsumerInstance{}
fmt.Println(string(httpClient.Requests[len(httpClient.Requests)-2].Body))
err = json.Unmarshal(httpClient.Requests[len(httpClient.Requests)-2].Body, &consInst)
err = json.Unmarshal(httpClient.Requests[len(httpClient.Requests)-3].Body, &consInst)
assert.Nil(t, err)

// Only label type set, label and asset type are equal
Expand Down
6 changes: 3 additions & 3 deletions pkg/apic/apiserviceinstance.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,16 +173,15 @@ func createInstanceEndpoint(endpoints []EndpointDefinition) ([]management.ApiSer
// Any endpoints provided from the ServiceBodyBuilder will override the endpoints found in the spec.
if len(endpoints) > 0 {
for _, endpointDef := range endpoints {
ep := management.ApiServiceInstanceSpecEndpoint{
endPoints = append(endPoints, management.ApiServiceInstanceSpecEndpoint{
Host: endpointDef.Host,
Port: endpointDef.Port,
Protocol: endpointDef.Protocol,
Routing: management.ApiServiceInstanceSpecRouting{
BasePath: endpointDef.BasePath,
Details: endpointDef.Details,
},
}
endPoints = append(endPoints, ep)
})
}
} else {
log.Debug("Processing API service instance with no endpoint")
Expand All @@ -198,6 +197,7 @@ func createInstanceEndpoint(endpoints []EndpointDefinition) ([]management.ApiSer
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-- {
// TODO: change to id
queryParams := map[string]string{
"query": "metadata.references.name==" + getRevisionPrefix(serviceBody) + "." + strconv.Itoa(i),
}
Expand Down
Loading