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

Add support for new tenant variables endpoints with environment scoping #301

Merged
merged 15 commits into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from 12 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
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.TenantProjectVariablesResource, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✔️

return newclient.GetByID[variables.TenantProjectVariablesResource](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.TenantCommonVariablesResource, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✔️

return newclient.GetByID[variables.TenantCommonVariablesResource](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.TenantProjectVariablesResource, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✔️

return newclient.Update[variables.TenantProjectVariablesResource](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.TenantCommonVariablesResource, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✔️

return newclient.Update[variables.TenantCommonVariablesResource](client, tenantCommonVariableTemplate, spaceID, tenantID, commonVariables)
}
40 changes: 40 additions & 0 deletions pkg/variables/tenant_common_variable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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 TenantCommonVariablesResource struct {
TenantID string `json:"TenantId,omitempty"`
CommonVariables []TenantCommonVariable `json:"CommonVariables,omitempty"`

resources.Resource
}

type TenantCommonVariable struct {
Id string `json:"Id,omitempty"`
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"`

resources.Resource
}
41 changes: 41 additions & 0 deletions pkg/variables/tenant_project_variable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
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 TenantProjectVariablesResource struct {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think our naming needs a bit of work. Resource has been superseded. TenantProjectVariable is a read type, whereas TenantProjectVariableCommand is the write type. Love to establish a convention so it's easy to understand what the types mean and when to use them without having to reason through where and how they are used.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than TenantProjectVariablesResource, how about TenantProjectVariableResponse? This would help to make it a bit clearer what the object is intended to be used for. Within that, we currently have TenantProjectVariable, which could be renamed to something similar to TenantProjectVariableValueResponse or TenantProjectVariableDetailsResponse

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Names updated based on the team discussion

TenantID string `json:"TenantId,omitempty"`
ProjectVariables []TenantProjectVariable `json:"ProjectVariables,omitempty"`

resources.Resource
}

type TenantProjectVariable struct {
Id string `json:"Id,omitempty"`
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"`

resources.Resource
}
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
}
Loading