Skip to content

Commit

Permalink
feat: deployment freezes (#282)
Browse files Browse the repository at this point in the history
  • Loading branch information
benPearce1 authored Nov 21, 2024
1 parent b215307 commit 2cac917
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pkg/client/octopusdeploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions pkg/deploymentfreezes/deploymentfreeze.go
Original file line number Diff line number Diff line change
@@ -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
}
10 changes: 10 additions & 0 deletions pkg/deploymentfreezes/deploymentfreeze_query.go
Original file line number Diff line number Diff line change
@@ -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"`
}
17 changes: 17 additions & 0 deletions pkg/deploymentfreezes/deploymentfreeze_service_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
111 changes: 111 additions & 0 deletions pkg/deploymentfreezes/deploymentfreezes_service.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 2cac917

Please sign in to comment.