Skip to content

Commit

Permalink
Merge branch 'APIGOV-28632' into APIGOV-29555
Browse files Browse the repository at this point in the history
  • Loading branch information
alrosca committed Feb 26, 2025
2 parents 40832da + c5cb3c3 commit 9d424b7
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 13 deletions.
4 changes: 4 additions & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ twistlock-traceability:on-schedule:
rules:
- !reference [.only-never-rule, rules]

merge-twistlock-scans-latest:
rules:
- !reference [.only-never-rule, rules]

upload-files-to-srm:
rules:
- !reference [.only-never-rule, rules]
Expand Down
1 change: 1 addition & 0 deletions pkg/agent/stream/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ func (s *StreamerClient) UpdateAgentStatus(state, prevState, message string) err
func (s *StreamerClient) writeStatusRequest(state, message string) error {
if s.canUpdateStatus() {
req := &proto.Request{
SelfLink: s.topicSelfLink,
RequestType: proto.RequestType_AGENT_STATUS.Enum(),
AgentStatus: &proto.AgentStatus{
State: state,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,11 @@ type APIServiceInstance struct {
//
// Compliance ApiServiceInstanceCompliance `json:"compliance"`
Compliance *ApiServiceInstanceCompliance `json:"compliance,omitempty"`
Lifecycle ApiServiceInstanceLifecycle `json:"lifecycle"`
Owner *apiv1.Owner `json:"owner"`
References ApiServiceInstanceReferences `json:"references"`
Lifecycle *ApiServiceInstanceLifecycle `json:"lifecycle,omitempty"`
// GENERATE: The following code has been modified after code generation
// Lifecycle ApiServiceInstanceLifecycle `json:"lifecycle"`
Owner *apiv1.Owner `json:"owner"`
References ApiServiceInstanceReferences `json:"references"`
// GENERATE: The following code has been modified after code generation
//
// Source ApiServiceInstanceSource `json:"source"`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ type DataplaneSpecAws struct {
AccessLogARN string `json:"accessLogARN,omitempty"`
// If true, the discovery agent will enable full transaction logging for discovered API stages
FullTransactionLogging bool `json:"fullTransactionLogging,omitempty"`
// The name of tag on AWS API Gateway Stage that holds mapped stage on Amplify Engage.
StageTagName string `json:"stageTagName,omitempty"`
}
22 changes: 17 additions & 5 deletions pkg/apic/apiserviceinstance.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ func (c *ServiceClient) buildAPIServiceInstance(
Owner: owner,
}
buildAPIServiceInstanceSourceSubResource(instance, serviceBody)
buildAPIServiceInstanceLifecycleSubResource(instance, serviceBody)

instDetails := util.MergeMapStringInterface(serviceBody.ServiceAgentDetails, serviceBody.InstanceAgentDetails)
details := buildAgentDetailsSubResource(serviceBody, false, instDetails)
Expand All @@ -120,6 +121,7 @@ func (c *ServiceClient) updateAPIServiceInstance(

instance.Owner = owner
buildAPIServiceInstanceSourceSubResource(instance, serviceBody)
buildAPIServiceInstanceLifecycleSubResource(instance, serviceBody)

details := util.MergeMapStringInterface(serviceBody.ServiceAgentDetails, serviceBody.InstanceAgentDetails)
util.SetAgentDetails(instance, buildAgentDetailsSubResource(serviceBody, false, details))
Expand Down Expand Up @@ -165,6 +167,14 @@ func buildAPIServiceInstanceSourceSubResource(instance *management.APIServiceIns
return nil
}

func buildAPIServiceInstanceLifecycleSubResource(instance *management.APIServiceInstance, serviceBody *ServiceBody) *management.ApiServiceInstanceSource {
lifecycle := serviceBody.GetInstanceLifeCycle()
if lifecycle != nil {
instance.Lifecycle = lifecycle
}
return nil
}

// processInstance - Creates or updates an API Service Instance based on the current API Service Revision.
func (c *ServiceClient) processInstance(serviceBody *ServiceBody) error {
endpoints, err := createInstanceEndpoint(serviceBody.Endpoints)
Expand All @@ -190,10 +200,6 @@ func (c *ServiceClient) processInstance(serviceBody *ServiceBody) error {
addSpecHashToResource(instance)

ri, err := c.CreateOrUpdateResource(instance)
if err == nil {
err = c.updateAPIServiceInstanceSubresources(ri, instance, serviceBody)
}

if err != nil {
if serviceBody.serviceContext.serviceAction == addAPI {
_, rollbackErr := c.rollbackAPIService(serviceBody.serviceContext.serviceName)
Expand All @@ -203,7 +209,10 @@ func (c *ServiceClient) processInstance(serviceBody *ServiceBody) error {
}
return err
}

err = c.updateAPIServiceInstanceSubresources(ri, instance, serviceBody)
if err != nil {
c.logger.WithField("resourceName", ri.Name).WithError(err).Warn("failed to update subresource")
}
c.caches.AddAPIServiceInstance(ri)
serviceBody.serviceContext.instanceName = instance.Name

Expand All @@ -215,6 +224,9 @@ func (c *ServiceClient) updateAPIServiceInstanceSubresources(ri apiv1.Interface,
if serviceBody.serviceContext.updateInstanceSource && instance.Source != nil {
subResources[management.ApiServiceInstanceSourceSubResourceName] = instance.Source
}
if serviceBody.instanceLifecycle != nil && instance.Lifecycle != nil && instance.Lifecycle.Stage != "" {
subResources[management.ApiServiceInstanceLifecycleSubResourceName] = instance.Lifecycle
}

if len(subResources) > 0 {
inst, _ := ri.AsInstance()
Expand Down
7 changes: 7 additions & 0 deletions pkg/apic/apiserviceinstance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ func TestServiceClient_buildAPIServiceInstance(t *testing.T) {
RevisionAgentDetails: map[string]interface{}{
"subresource_revision_key": "value",
},
instanceLifecycle: &management.ApiServiceInstanceLifecycle{
Stage: "stage",
ReleaseState: management.ApiServiceInstanceLifecycleReleaseState{
Name: "active",
},
},
}

tags := []string{"tag1_value1", "tag2_value2"}
Expand Down Expand Up @@ -80,6 +86,7 @@ func TestServiceClient_buildAPIServiceInstance(t *testing.T) {
assert.NotContains(t, inst.Attributes, defs.AttrExternalAPIID)
assert.NotContains(t, inst.Attributes, defs.AttrExternalAPIName)
assert.NotContains(t, inst.Attributes, defs.AttrCreatedBy)
assert.NotNil(t, inst.Lifecycle)

assert.Equal(t, inst.Spec.Endpoint, ep)

Expand Down
5 changes: 5 additions & 0 deletions pkg/apic/servicebody.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ type ServiceBody struct {
referencedServiceName string
referencedInstanceName string
logger log.FieldLogger
instanceLifecycle *management.ApiServiceInstanceLifecycle
}

// SetAccessRequestDefinitionName - set the name of the access request definition for this service body
Expand Down Expand Up @@ -166,3 +167,7 @@ func (s *ServiceBody) GetReferencedServiceName() string {
func (s *ServiceBody) GetReferenceInstanceName() string {
return s.referencedInstanceName
}

func (s *ServiceBody) GetInstanceLifeCycle() *management.ApiServiceInstanceLifecycle {
return s.instanceLifecycle
}
15 changes: 15 additions & 0 deletions pkg/apic/servicebuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"regexp"

management "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/management/v1alpha1"
"github.com/Axway/agent-sdk/pkg/apic/provisioning"
"github.com/Axway/agent-sdk/pkg/config"
"github.com/Axway/agent-sdk/pkg/util/log"
Expand Down Expand Up @@ -66,6 +67,7 @@ type ServiceBuilder interface {
SetSourceDataplaneType(dataplaneType DataplaneType, isDesign bool) ServiceBuilder
SetReferenceServiceName(serviceName, envName string) ServiceBuilder
SetReferenceInstanceName(instanceName, envName string) ServiceBuilder
SetInstanceLifecycle(stage, releaseState, message string) ServiceBuilder

Build() (ServiceBody, error)
}
Expand Down Expand Up @@ -431,3 +433,16 @@ func (b *serviceBodyBuilder) SetReferenceInstanceName(instanceName, envName stri
}
return b
}

func (b *serviceBodyBuilder) SetInstanceLifecycle(stage, releaseState, message string) ServiceBuilder {
if stage != "" && releaseState != "" {
b.serviceBody.instanceLifecycle = &management.ApiServiceInstanceLifecycle{
Stage: stage,
ReleaseState: management.ApiServiceInstanceLifecycleReleaseState{
Name: releaseState,
Message: message,
},
}
}
return b
}
12 changes: 8 additions & 4 deletions pkg/apic/servicebuilder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ func TestServiceBodySetters(t *testing.T) {
SetRevisionAgentDetails(revDetails).
SetReferenceServiceName("refSvc", "refEnv").
SetReferenceInstanceName("refInstance", "refEnv").
SetIgnoreSpecBasedCreds(true)
SetIgnoreSpecBasedCreds(true).
SetInstanceLifecycle("stage", "active", "").
SetServiceEndpoints(ep)

sb, err := serviceBuilder.
SetServiceEndpoints(ep).
Build()
sb, err := serviceBuilder.Build()

assert.Nil(t, err)
assert.NotNil(t, sb)
Expand Down Expand Up @@ -132,6 +132,10 @@ func TestServiceBodySetters(t *testing.T) {
assert.Equal(t, "refEnv/refSvc", sb.GetReferencedServiceName())
assert.Equal(t, "refEnv/refInstance", sb.GetReferenceInstanceName())
assert.Equal(t, true, sb.ignoreSpecBasesCreds)
instanceLifecycle := sb.GetInstanceLifeCycle()
assert.NotNil(t, instanceLifecycle)
assert.Equal(t, "stage", instanceLifecycle.Stage)
assert.Equal(t, "active", instanceLifecycle.ReleaseState.Name)

sb, err = serviceBuilder.
SetSourceDataplaneType(GitHub, true).
Expand Down
18 changes: 17 additions & 1 deletion pkg/watchmanager/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ import (
"github.com/golang-jwt/jwt"
)

const (
initDuration = 30 * time.Second
)

type clientConfig struct {
errors chan error
events chan *proto.Event
Expand Down Expand Up @@ -55,7 +59,7 @@ func newWatchClient(cc grpc.ClientConnInterface, clientCfg clientConfig, newClie
isRunning: true,
stream: stream,
streamCtx: streamCtx,
timer: time.NewTimer(0),
timer: time.NewTimer(initDuration),
}

return client, nil
Expand Down Expand Up @@ -91,9 +95,21 @@ func (c *watchClient) processRequest() error {
}
lock := createInitialRequestLock()
go c.requestLoop(lock)

// writes the initial watch request and resets the timer
err := c.initialRequest()
if err != nil {
c.handleError(err)
return err
}

return lock.wait()
}

func (c *watchClient) initialRequest() error {
return c.createTokenRefreshRequest()
}

func (c *watchClient) requestLoop(rl *initialRequestLock) {
var err error
defer func() {
Expand Down
14 changes: 14 additions & 0 deletions scripts/apiserver/modify_models.sh
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,20 @@ $SED -i "/ApiServiceInstanceCompliance\s/a ${REPLACE}" ${MODEL_PATH}/APIServiceI
# reformat the code
go fmt ${MODEL_PATH}/APIServiceInstance.go

######################
# For APIServiceInstance.go, we want to turn "Lifecycle ApiServiceInstanceLifecycle `json:"lifecycle"`" into
# "Lifecycle *ApiServiceInstanceLifecycle `json:"lifecycle,omitempty"`"
######################
SEARCH="\s*Lifecycle\s*ApiServiceInstanceLifecycle.*"
REPLACE="Lifecycle *ApiServiceInstanceLifecycle \`json:\"lifecycle,omitempty\"\`"
# add a comment to the code
$SED -i -e "/${SEARCH}/i ${COMMENT}" ${MODEL_PATH}/APIServiceInstance.go
# comment out the line we're changing
$SED -i -e "s/${SEARCH}/\/\/ &/" ${MODEL_PATH}/APIServiceInstance.go
# add in the new line we want
$SED -i "/ApiServiceInstanceCompliance\s/a ${REPLACE}" ${MODEL_PATH}/APIServiceInstance.go
# reformat the code
go fmt ${MODEL_PATH}/APIServiceInstance.go

######################
# For APIServiceInstance.go, we want to turn "Source ApiServiceInstanceSource `json:"source"`" into
Expand Down

0 comments on commit 9d424b7

Please sign in to comment.