|
| 1 | +package apidef |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "github.com/TykTechnologies/tyk/apidef" |
| 7 | + "github.com/TykTechnologies/tyk/common/option" |
| 8 | + "net/url" |
| 9 | +) |
| 10 | + |
| 11 | +const ( |
| 12 | + // BaseAPIID represents the parameter for the base API identifier. |
| 13 | + // This is used to identify the original API that a version is based on. |
| 14 | + BaseAPIID VersionParameter = iota |
| 15 | + |
| 16 | + // BaseAPIVersionName represents the parameter for the base API version name. |
| 17 | + // This is used to specify the name of the version in the base API. |
| 18 | + BaseAPIVersionName |
| 19 | + |
| 20 | + // NewVersionName represents the parameter for the new version name. |
| 21 | + // This is used when creating a new version of an API. |
| 22 | + NewVersionName |
| 23 | + |
| 24 | + // SetDefault represents the parameter that determines if a version should be set as default. |
| 25 | + // When true, the new version will be marked as the default version for the API. |
| 26 | + SetDefault |
| 27 | + |
| 28 | + // paramCount is used internally to track the number of version parameters. |
| 29 | + paramCount |
| 30 | +) |
| 31 | + |
| 32 | +const TrueString = "true" |
| 33 | + |
| 34 | +var ( |
| 35 | + // ErrNewVersionRequired is returned when a new version name is required but not provided. |
| 36 | + // This error occurs during validation when attempting to create a new API version. |
| 37 | + ErrNewVersionRequired = errors.New("The new version name is required") |
| 38 | +) |
| 39 | + |
| 40 | +// VersionParameter represents the type of parameter used in API version configuration. |
| 41 | +// It defines the possible parameters that can be used when working with API versions. |
| 42 | +type VersionParameter int |
| 43 | + |
| 44 | +// String returns the string representation of a VersionParameter. |
| 45 | +// It converts the numeric parameter value to its corresponding string identifier. |
| 46 | +func (v VersionParameter) String() string { |
| 47 | + return []string{"base_api_id", "base_api_version_name", "new_version_name", "set_default"}[v] |
| 48 | +} |
| 49 | + |
| 50 | +// AllVersionParameters returns a slice containing all available version parameters. |
| 51 | +// This is useful for iterating through all possible version parameters. |
| 52 | +func AllVersionParameters() []VersionParameter { |
| 53 | + params := make([]VersionParameter, paramCount) |
| 54 | + for i := range params { |
| 55 | + params[i] = VersionParameter(i) |
| 56 | + } |
| 57 | + |
| 58 | + return params |
| 59 | +} |
| 60 | + |
| 61 | +// VersionQueryParameters holds the version-related parameters extracted from HTTP requests. |
| 62 | +// It provides methods to access and validate these parameters. |
| 63 | +type VersionQueryParameters struct { |
| 64 | + versionParams map[string]string |
| 65 | +} |
| 66 | + |
| 67 | +// Validate checks if the version parameters are valid. |
| 68 | +// It takes a function that checks if the base API exists and returns an error if validation fails. |
| 69 | +// The doesBaseApiExists function should return whether the base API exists and its name. |
| 70 | +func (v *VersionQueryParameters) Validate(doesBaseApiExists func() (bool, string)) error { |
| 71 | + if v.IsEmpty(BaseAPIID) { |
| 72 | + return nil |
| 73 | + } |
| 74 | + baseID := v.Get(BaseAPIID) |
| 75 | + |
| 76 | + if v.IsEmpty(NewVersionName) { |
| 77 | + return ErrNewVersionRequired |
| 78 | + } |
| 79 | + |
| 80 | + exists, baseName := doesBaseApiExists() |
| 81 | + if !exists { |
| 82 | + return fmt.Errorf("%s is not a valid Base API version", baseID) |
| 83 | + } |
| 84 | + |
| 85 | + if v.IsEmpty(BaseAPIVersionName) && baseName == "" { |
| 86 | + return fmt.Errorf("you need to provide a version name for the new Base API: %s", baseID) |
| 87 | + } |
| 88 | + |
| 89 | + return nil |
| 90 | +} |
| 91 | + |
| 92 | +// IsEmpty checks if a specific version parameter is empty or not set. |
| 93 | +// Returns true if the parameter is empty, false otherwise. |
| 94 | +func (v *VersionQueryParameters) IsEmpty(param VersionParameter) bool { |
| 95 | + return v.versionParams[param.String()] == "" |
| 96 | +} |
| 97 | + |
| 98 | +// Get retrieves the value of a specific version parameter. |
| 99 | +// Returns the string value of the parameter. |
| 100 | +func (v *VersionQueryParameters) Get(param VersionParameter) string { |
| 101 | + return v.versionParams[param.String()] |
| 102 | +} |
| 103 | + |
| 104 | +// NewVersionQueryParameters creates a new VersionQueryParameters instance from an HTTP request. |
| 105 | +// It extracts all version-related parameters from the request's URL query. |
| 106 | +func NewVersionQueryParameters(query url.Values) *VersionQueryParameters { |
| 107 | + versionParams := &VersionQueryParameters{ |
| 108 | + versionParams: make(map[string]string, paramCount), |
| 109 | + } |
| 110 | + |
| 111 | + for _, param := range AllVersionParameters() { |
| 112 | + paramName := param.String() |
| 113 | + versionParams.versionParams[paramName] = query.Get(paramName) |
| 114 | + } |
| 115 | + |
| 116 | + return versionParams |
| 117 | +} |
| 118 | + |
| 119 | +// WithVersionName creates an option that sets the version name in a VersionDefinition. |
| 120 | +func WithVersionName(name string) option.Option[apidef.VersionDefinition] { |
| 121 | + return func(version *apidef.VersionDefinition) { |
| 122 | + version.Name = name |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +// WithBaseID creates an option that sets the version baseID in a VersionDefinition. |
| 127 | +func WithBaseID(id string) option.Option[apidef.VersionDefinition] { |
| 128 | + return func(version *apidef.VersionDefinition) { |
| 129 | + version.BaseID = id |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +// AddVersion creates an option that adds a version mapping to a VersionDefinition. |
| 134 | +// It associates a version name with an API ID in the Versions map. |
| 135 | +func AddVersion(versionName, apiID string) option.Option[apidef.VersionDefinition] { |
| 136 | + return func(vd *apidef.VersionDefinition) { |
| 137 | + vd.Versions[versionName] = apiID |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +// SetAsDefault creates an option that marks a specific version as the default. |
| 142 | +// This sets the Default field in the VersionDefinition to the specified version name. |
| 143 | +func SetAsDefault(versionName string) option.Option[apidef.VersionDefinition] { |
| 144 | + return func(vd *apidef.VersionDefinition) { |
| 145 | + vd.Default = versionName |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +// ConfigureVersionDefinition sets up the version definition with default values if not already set. |
| 150 | +// It applies the provided parameters to configure the version definition and ensures |
| 151 | +// that required fields have appropriate values. |
| 152 | +func ConfigureVersionDefinition(def apidef.VersionDefinition, params *VersionQueryParameters, newApiID string) apidef.VersionDefinition { |
| 153 | + opts := make([]option.Option[apidef.VersionDefinition], 0) |
| 154 | + |
| 155 | + if !params.IsEmpty(BaseAPIID) { |
| 156 | + def.Enabled = true |
| 157 | + |
| 158 | + if !params.IsEmpty(BaseAPIVersionName) { |
| 159 | + opts = append(opts, WithVersionName(params.Get(BaseAPIVersionName))) |
| 160 | + } |
| 161 | + |
| 162 | + if !params.IsEmpty(SetDefault) { |
| 163 | + setDefault := params.Get(SetDefault) |
| 164 | + if setDefault == TrueString { |
| 165 | + opts = append(opts, SetAsDefault(params.Get(NewVersionName))) |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + if !params.IsEmpty(BaseAPIID) { |
| 170 | + opts = append(opts, WithBaseID(params.Get(BaseAPIID))) |
| 171 | + } |
| 172 | + |
| 173 | + opts = append(opts, AddVersion(params.Get(NewVersionName), newApiID)) |
| 174 | + } |
| 175 | + |
| 176 | + // When baseAPIID is missing in the request params, and it's versioning is enabled then set versioning ID as APIID |
| 177 | + if params.IsEmpty(BaseAPIID) && def.BaseID == "" { |
| 178 | + def.BaseID = newApiID |
| 179 | + } |
| 180 | + |
| 181 | + if def.Key == "" { |
| 182 | + def.Key = apidef.DefaultAPIVersionKey |
| 183 | + } |
| 184 | + |
| 185 | + if def.Location == "" { |
| 186 | + def.Location = apidef.HeaderLocation |
| 187 | + } |
| 188 | + |
| 189 | + if def.Default == "" { |
| 190 | + def.Default = apidef.Self |
| 191 | + } |
| 192 | + |
| 193 | + if def.Versions == nil { |
| 194 | + def.Versions = make(map[string]string) |
| 195 | + } |
| 196 | + |
| 197 | + return *option.New(opts).Build(def) |
| 198 | +} |
0 commit comments