Skip to content

Commit

Permalink
Merge pull request #301 from OctopusDeploy/fnm/scoped-common-tenant-v…
Browse files Browse the repository at this point in the history
…ariables

Add support for new tenant variables endpoints with environment scoping
  • Loading branch information
bec-callow-oct authored Feb 14, 2025
2 parents a0df467 + 02b37b4 commit faca5fe
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 0 deletions.
14 changes: 14 additions & 0 deletions pkg/configuration/feature_toggle_configuration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package configuration

type FeatureToggleConfigurationQuery struct {
Name string `uri:"Name,omitempty" url:"Name,omitempty"`
}

type FeatureToggleConfigurationResponse struct {
FeatureToggles []ConfiguredFeatureToggle `json:"FeatureToggles"`
}

type ConfiguredFeatureToggle struct {
Name string `json:"Name"`
IsEnabled bool `json:"IsEnabled"`
}
13 changes: 13 additions & 0 deletions pkg/configuration/feature_toggle_configuration_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package configuration

import (
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/newclient"
)

type FeatureToggleConfigurationService struct{}

const template = "/api/configuration/feature-toggles{?Name}"

func Get(client newclient.Client, query *FeatureToggleConfigurationQuery) (*FeatureToggleConfigurationResponse, error) {
return newclient.GetByQueryWithoutSpace[FeatureToggleConfigurationResponse](client, template, query)
}
21 changes: 21 additions & 0 deletions pkg/newclient/crud.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,27 @@ func GetByQuery[TResource any](client Client, template string, spaceID string, q
return res, nil
}

// GetByQueryWithoutSpace returns a resource based on the criteria defined by
// its input query parameter.
func GetByQueryWithoutSpace[TResource any](client Client, template string, query any) (*TResource, error) {
values, _ := uritemplates.Struct2map(query)
if values == nil {
values = map[string]any{}
}

path, err := client.URITemplateCache().Expand(template, values)
if err != nil {
return nil, err
}

res, err := Get[TResource](client.HttpSession(), path)
if err != nil {
return nil, err
}

return res, nil
}

// GetByID returns the resource that matches the input ID.
func GetByID[TResource any](client Client, template string, spaceID string, ID string) (*TResource, error) {
if ID == "" {
Expand Down
23 changes: 23 additions & 0 deletions pkg/tenants/tenant_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,3 +314,26 @@ func DeleteByID(client newclient.Client, spaceID string, ID string) error {
func GetAll(client newclient.Client, spaceID string) ([]*Tenant, error) {
return newclient.GetAll[Tenant](client, template, spaceID)
}

const tenantProjectVariableTemplate = "/api/{spaceId}/tenants/{id}/projectvariables"
const tenantCommonVariableTemplate = "/api/{spaceId}/tenants/{id}/commonvariables"

// GetProjectVariables returns all tenant project variables. If an error occurs, it returns nil.
func GetProjectVariables(client newclient.Client, spaceID string, tenantID string) (*variables.TenantProjectVariablesResponse, error) {
return newclient.GetByID[variables.TenantProjectVariablesResponse](client, tenantProjectVariableTemplate, spaceID, tenantID)
}

// GetCommonVariables returns all tenant common variables. If an error occurs, it returns nil.
func GetCommonVariables(client newclient.Client, spaceID string, tenantID string) (*variables.TenantCommonVariablesResponse, error) {
return newclient.GetByID[variables.TenantCommonVariablesResponse](client, tenantCommonVariableTemplate, spaceID, tenantID)
}

// UpdateProjectVariables modifies tenant project variables based on the ones provided as input.
func UpdateProjectVariables(client newclient.Client, spaceID string, tenantID string, projectVariables *variables.ModifyTenantProjectVariablesCommand) (*variables.TenantProjectVariablesResponse, error) {
return newclient.Update[variables.TenantProjectVariablesResponse](client, tenantProjectVariableTemplate, spaceID, tenantID, projectVariables)
}

// UpdateCommonVariables modifies tenant common variables based on the ones provided as input.
func UpdateCommonVariables(client newclient.Client, spaceID string, tenantID string, commonVariables *variables.ModifyTenantCommonVariablesCommand) (*variables.TenantCommonVariablesResponse, error) {
return newclient.Update[variables.TenantCommonVariablesResponse](client, tenantCommonVariableTemplate, spaceID, tenantID, commonVariables)
}
37 changes: 37 additions & 0 deletions pkg/variables/tenant_common_variable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package variables

import (
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/actiontemplates"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/core"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/resources"
)

type TenantCommonVariablesResponse struct {
TenantID string `json:"TenantId,omitempty"`
CommonVariables []TenantCommonVariable `json:"CommonVariables,omitempty"`

resources.Resource
}

type TenantCommonVariable struct {
LibraryVariableSetId string `json:"LibraryVariableSetId"`
LibraryVariableSetName string `json:"LibraryVariableSetName,omitempty"`
TemplateID string `json:"TemplateId"`
Template actiontemplates.ActionTemplateParameter `json:"Template"`
Value core.PropertyValue `json:"Value"`
Scope TenantVariableScope `json:"Scope"`

resources.Resource
}

type ModifyTenantCommonVariablesCommand struct {
Variables []TenantCommonVariableCommand `json:"Variables"`
}

type TenantCommonVariableCommand struct {
ID string `json:"Id,omitempty"`
LibraryVariableSetId string `json:"LibraryVariableSetId"`
TemplateID string `json:"TemplateId"`
Value core.PropertyValue `json:"Value"`
Scope TenantVariableScope `json:"Scope"`
}
38 changes: 38 additions & 0 deletions pkg/variables/tenant_project_variable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package variables

import (
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/actiontemplates"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/core"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/resources"
)

type TenantProjectVariablesResponse struct {
TenantID string `json:"TenantId,omitempty"`
ProjectVariables []TenantProjectVariable `json:"ProjectVariables,omitempty"`

resources.Resource
}

type TenantProjectVariable struct {
ProjectID string `json:"ProjectId"`
ProjectName string `json:"ProjectName,omitempty"`
TemplateID string `json:"TemplateId"`
Template actiontemplates.ActionTemplateParameter `json:"Template"`
Value core.PropertyValue `json:"Value"`
Scope TenantVariableScope `json:"Scope"`
Links map[string]string `json:"Links,omitempty"`

resources.Resource
}

type ModifyTenantProjectVariablesCommand struct {
Variables []TenantProjectVariableCommand `json:"Variables"`
}

type TenantProjectVariableCommand struct {
ID string `json:"Id,omitempty"`
ProjectID string `json:"ProjectId"`
TemplateID string `json:"TemplateId"`
Value core.PropertyValue `json:"Value"`
Scope TenantVariableScope `json:"Scope"`
}
9 changes: 9 additions & 0 deletions pkg/variables/tenant_variable_scope.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package variables

import "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/resources"

type TenantVariableScope struct {
EnvironmentIds []string `json:"EnvironmentIds"`

resources.Resource
}

0 comments on commit faca5fe

Please sign in to comment.