github.com/Axway/agent-sdk@v1.1.101/pkg/apic/servicebody.go (about) 1 package apic 2 3 import ( 4 management "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/management/v1alpha1" 5 "github.com/Axway/agent-sdk/pkg/apic/provisioning" 6 "github.com/Axway/agent-sdk/pkg/util/log" 7 ) 8 9 // APIKeyInfo - 10 type APIKeyInfo struct { 11 Name string 12 Location string 13 } 14 15 // ServiceBody - details about a service to create 16 type ServiceBody struct { 17 NameToPush string 18 APIName string 19 RestAPIID string 20 PrimaryKey string 21 URL string 22 Stage string 23 StageDescriptor string 24 StageDisplayName string 25 Description string 26 Version string 27 AuthPolicy string 28 authPolicies []string 29 apiKeyInfo []APIKeyInfo 30 scopes map[string]string 31 SpecDefinition []byte 32 Documentation []byte 33 Tags map[string]interface{} 34 Image string 35 ImageContentType string 36 CreatedBy string 37 ResourceContentType string 38 ResourceType string 39 SubscriptionName string 40 APIUpdateSeverity string 41 State string 42 Status string 43 ServiceAttributes map[string]string 44 RevisionAttributes map[string]string 45 InstanceAttributes map[string]string 46 ServiceAgentDetails map[string]interface{} 47 InstanceAgentDetails map[string]interface{} 48 RevisionAgentDetails map[string]interface{} 49 serviceContext serviceContext 50 Endpoints []EndpointDefinition 51 UnstructuredProps *UnstructuredProperties 52 TeamName string 53 teamID string 54 credentialRequestPolicies []string 55 ardName string 56 uniqueARD bool 57 ignoreSpecBasesCreds bool 58 specHash string 59 specVersion string 60 accessRequestDefinition *management.AccessRequestDefinition 61 specHashes map[string]interface{} // map of hash values to revision names 62 requestDefinitionsAllowed bool // used to validate if the instance can have request definitions or not. Use case example - v7 unpublished, remove request definitions 63 dataplaneType DataplaneType 64 isDesignDataplane bool 65 referencedServiceName string 66 referencedInstanceName string 67 logger log.FieldLogger 68 } 69 70 // SetAccessRequestDefinitionName - set the name of the access request definition for this service body 71 func (s *ServiceBody) SetAccessRequestDefinitionName(ardName string, isUnique bool) { 72 s.ardName = ardName 73 s.uniqueARD = isUnique 74 } 75 76 func (s *ServiceBody) SetIgnoreSpecBasedCreds(ignore bool) { 77 s.ignoreSpecBasesCreds = ignore 78 } 79 80 // GetAuthPolicies - returns the array of all auth policies in the ServiceBody 81 func (s *ServiceBody) GetAuthPolicies() []string { 82 return s.authPolicies 83 } 84 85 // GetAPIKeyInfo - returns the array of locations and argument names for the api key 86 func (s *ServiceBody) GetAPIKeyInfo() []APIKeyInfo { 87 return s.apiKeyInfo 88 } 89 90 // GetScopes - returns the array of scopes for this service instance 91 func (s *ServiceBody) GetScopes() map[string]string { 92 return s.scopes 93 } 94 95 // GetCredentialRequestDefinitions - returns the array of all credential request policies 96 func (s *ServiceBody) GetCredentialRequestDefinitions(allowedOAuthMethods []string) []string { 97 if len(s.credentialRequestPolicies) > 0 || s.ignoreSpecBasesCreds { 98 return s.credentialRequestPolicies 99 } 100 for _, policy := range s.authPolicies { 101 if policy == Basic { 102 s.credentialRequestPolicies = append(s.credentialRequestPolicies, provisioning.BasicAuthCRD) 103 } 104 if policy == Apikey { 105 s.credentialRequestPolicies = append(s.credentialRequestPolicies, provisioning.APIKeyCRD) 106 } 107 if policy == Oauth { 108 oauthCRDs := []string{provisioning.OAuthPublicKeyCRD, provisioning.OAuthSecretCRD} 109 if len(allowedOAuthMethods) > 0 { 110 oauthCRDs = allowedOAuthMethods 111 } 112 s.credentialRequestPolicies = append(s.credentialRequestPolicies, oauthCRDs...) 113 } 114 } 115 return s.credentialRequestPolicies 116 } 117 118 func (s *ServiceBody) setAccessRequestDefinition(accessRequestDefinition *management.AccessRequestDefinition) (*management.AccessRequestDefinition, error) { 119 s.accessRequestDefinition = accessRequestDefinition 120 return s.accessRequestDefinition, nil 121 } 122 123 // GetAccessRequestDefinition - 124 func (s *ServiceBody) GetAccessRequestDefinition() *management.AccessRequestDefinition { 125 return s.accessRequestDefinition 126 } 127 128 func (s *ServiceBody) createAccessRequestDefinition() error { 129 if s.ignoreSpecBasesCreds { 130 s.logger.WithField("ardName", s.ardName).Debug("skipping registering new ARD") 131 return nil 132 } 133 oauthScopes := make([]string, 0) 134 for scope := range s.GetScopes() { 135 oauthScopes = append(oauthScopes, scope) 136 } 137 if len(oauthScopes) > 0 { 138 // sort the strings for consistent specs 139 _, err := provisioning.NewAccessRequestBuilder(s.setAccessRequestDefinition).Register() 140 if err != nil { 141 return err 142 } 143 } 144 return nil 145 } 146 147 // GetSpecVersion - returns version parsed from the spec 148 func (s *ServiceBody) GetSpecVersion() string { 149 return s.specVersion 150 } 151 152 // GetDataplaneType - returns dataplane type 153 func (s *ServiceBody) GetDataplaneType() DataplaneType { 154 return s.dataplaneType 155 } 156 157 // IsDesignDataplane - returns true for design dataplane 158 func (s *ServiceBody) IsDesignDataplane() bool { 159 return s.isDesignDataplane 160 } 161 162 func (s *ServiceBody) GetReferencedServiceName() string { 163 return s.referencedServiceName 164 } 165 166 func (s *ServiceBody) GetReferenceInstanceName() string { 167 return s.referencedInstanceName 168 }