From 2cac9177944cd332fb2cefbd8a07d47da3257c33 Mon Sep 17 00:00:00 2001 From: Ben Pearce Date: Fri, 22 Nov 2024 09:57:27 +1000 Subject: [PATCH] feat: deployment freezes (#282) --- pkg/client/octopusdeploy.go | 2 + pkg/deploymentfreezes/deploymentfreeze.go | 25 ++++ .../deploymentfreeze_query.go | 10 ++ .../deploymentfreeze_service_test.go | 17 +++ .../deploymentfreezes_service.go | 111 ++++++++++++++++++ 5 files changed, 165 insertions(+) create mode 100644 pkg/deploymentfreezes/deploymentfreeze.go create mode 100644 pkg/deploymentfreezes/deploymentfreeze_query.go create mode 100644 pkg/deploymentfreezes/deploymentfreeze_service_test.go create mode 100644 pkg/deploymentfreezes/deploymentfreezes_service.go diff --git a/pkg/client/octopusdeploy.go b/pkg/client/octopusdeploy.go index 295992d8..ddb4e23a 100644 --- a/pkg/client/octopusdeploy.go +++ b/pkg/client/octopusdeploy.go @@ -23,6 +23,7 @@ import ( "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/constants" "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/credentials" "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/dashboard" + "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/deploymentfreezes" "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/deployments" "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/environments" "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/events" @@ -94,6 +95,7 @@ type Client struct { GitCredentials *credentials.Service DashboardConfigurations *dashboard.DashboardConfigurationService Dashboards *dashboard.DashboardService + DeploymentFreezes *deploymentfreezes.DeploymentFreezeService DeploymentProcesses *deployments.DeploymentProcessService Deployments *deployments.DeploymentService DynamicExtensions *extensions.DynamicExtensionService diff --git a/pkg/deploymentfreezes/deploymentfreeze.go b/pkg/deploymentfreezes/deploymentfreeze.go new file mode 100644 index 00000000..d84ab00e --- /dev/null +++ b/pkg/deploymentfreezes/deploymentfreeze.go @@ -0,0 +1,25 @@ +package deploymentfreezes + +import ( + "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/resources" + "time" +) + +type DeploymentFreezes resources.Resources[DeploymentFreeze] + +type DeploymentFreeze struct { + Name string `json:"Name" validate:"required"` + Start *time.Time `json:"Start,required"` + End *time.Time `json:"End,required"` + ProjectEnvironmentScope map[string][]string `json:"ProjectEnvironmentScope,omitempty"` + + resources.Resource +} + +func (d *DeploymentFreeze) GetName() string { + return d.Name +} + +func (d *DeploymentFreeze) SetName(name string) { + d.Name = name +} diff --git a/pkg/deploymentfreezes/deploymentfreeze_query.go b/pkg/deploymentfreezes/deploymentfreeze_query.go new file mode 100644 index 00000000..fbea045c --- /dev/null +++ b/pkg/deploymentfreezes/deploymentfreeze_query.go @@ -0,0 +1,10 @@ +package deploymentfreezes + +type DeploymentFreezeQuery struct { + IncludeComplete bool `uri:"includeComplete,omitempty"` + ProjectIds []string `uri:"projectIds,omitempty"` + EnvironmentIds []string `uri:"environmentIds,omitempty"` + IDs []string `uri:"ids,omitempty"` + Skip int `uri:"skip,omitempty"` + Take int `uri:"take,omitempty"` +} diff --git a/pkg/deploymentfreezes/deploymentfreeze_service_test.go b/pkg/deploymentfreezes/deploymentfreeze_service_test.go new file mode 100644 index 00000000..ee88694b --- /dev/null +++ b/pkg/deploymentfreezes/deploymentfreeze_service_test.go @@ -0,0 +1,17 @@ +package deploymentfreezes + +import ( + "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/newclient" + "github.com/stretchr/testify/require" + "testing" +) + +func createClient() newclient.Client { + return newclient.NewClient(&newclient.HttpSession{}) +} + +func TestAddParameterValidation(t *testing.T) { + client := createClient() + _, err := Add(client, nil) + require.Error(t, err) +} diff --git a/pkg/deploymentfreezes/deploymentfreezes_service.go b/pkg/deploymentfreezes/deploymentfreezes_service.go new file mode 100644 index 00000000..ff826b5c --- /dev/null +++ b/pkg/deploymentfreezes/deploymentfreezes_service.go @@ -0,0 +1,111 @@ +package deploymentfreezes + +import ( + "math" + + "github.com/OctopusDeploy/go-octopusdeploy/v2/internal" + "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/newclient" +) + +const template = "/api/deploymentfreezes{/id}{?skip,take,ids,projectIds,tenantIds,environmentIds,includeComplete,status}" + +type DeploymentFreezeService struct { +} + +func Get(client newclient.Client, deploymentFreezesQuery *DeploymentFreezeQuery) (*DeploymentFreezes, error) { + path, err := client.URITemplateCache().Expand(template, deploymentFreezesQuery) + if err != nil { + return nil, err + } + + res, err := newclient.Get[DeploymentFreezes](client.HttpSession(), path) + if err != nil { + return &DeploymentFreezes{}, err + } + + return res, nil +} + +func GetById(client newclient.Client, id string) (*DeploymentFreeze, error) { + path, err := client.URITemplateCache().Expand(template, map[string]any{ + "id": id, + }) + if err != nil { + return nil, err + } + + res, err := newclient.Get[DeploymentFreeze](client.HttpSession(), path) + if err != nil { + return &DeploymentFreeze{}, err + } + + return res, nil + +} + +func GetAll(client newclient.Client) ([]*DeploymentFreeze, error) { + path, err := client.URITemplateCache().Expand(template, &DeploymentFreezeQuery{Skip: 0, Take: math.MaxInt32}) + if err != nil { + return nil, err + } + + res, err := newclient.Get[DeploymentFreezes](client.HttpSession(), path) + + freezes := make([]*DeploymentFreeze, 0) + for _, freeze := range res.Items { + freezes = append(freezes, &freeze) + } + + return freezes, nil +} + +func Add(client newclient.Client, deploymentFreeze *DeploymentFreeze) (*DeploymentFreeze, error) { + if deploymentFreeze == nil { + return nil, internal.CreateRequiredParameterIsEmptyOrNilError("deploymentFreeze") + } + + path, err := client.URITemplateCache().Expand(template, deploymentFreeze) + if err != nil { + return nil, err + } + + res, err := newclient.Post[DeploymentFreeze](client.HttpSession(), path, deploymentFreeze) + if err != nil { + return nil, err + } + return res, nil +} + +func Update(client newclient.Client, deploymentFreeze *DeploymentFreeze) (*DeploymentFreeze, error) { + if deploymentFreeze == nil { + return nil, internal.CreateRequiredParameterIsEmptyOrNilError("deploymentFreeze") + } + + path, err := client.URITemplateCache().Expand(template, map[string]any{"id": deploymentFreeze.ID}) + if err != nil { + return nil, err + } + + res, err := newclient.Put[DeploymentFreeze](client.HttpSession(), path, deploymentFreeze) + if err != nil { + return nil, err + } + return res, nil +} + +func Delete(client newclient.Client, deploymentFreeze *DeploymentFreeze) error { + if deploymentFreeze == nil { + return internal.CreateRequiredParameterIsEmptyOrNilError("deploymentFreeze") + } + + path, err := client.URITemplateCache().Expand(template, map[string]any{"id": deploymentFreeze.ID}) + if err != nil { + return err + } + + err = newclient.Delete(client.HttpSession(), path) + if err != nil { + return err + } + return nil +}