From 20d8b59843e75dbf0132506c0978c9fa329734b2 Mon Sep 17 00:00:00 2001 From: Simon Wistow Date: Wed, 28 Aug 2024 06:17:08 +1000 Subject: [PATCH] Add support for 'environments' while activating and deactivating service-versions. Add 'environments' details to service and service-version API responses. --- fastly/service_details.go | 44 ++++++++++++++++++---------------- fastly/service_version.go | 50 +++++++++++++++++++++++++++++---------- 2 files changed, 60 insertions(+), 34 deletions(-) diff --git a/fastly/service_details.go b/fastly/service_details.go index 5b4dbc407..a0490e904 100644 --- a/fastly/service_details.go +++ b/fastly/service_details.go @@ -7,31 +7,33 @@ import ( // Service represents a server response from the Fastly API. type Service struct { - ActiveVersion *int `mapstructure:"version"` - Comment *string `mapstructure:"comment"` - CreatedAt *time.Time `mapstructure:"created_at"` - CustomerID *string `mapstructure:"customer_id"` - DeletedAt *time.Time `mapstructure:"deleted_at"` - ServiceID *string `mapstructure:"id"` - Name *string `mapstructure:"name"` - Type *string `mapstructure:"type"` - UpdatedAt *time.Time `mapstructure:"updated_at"` - Versions []*Version `mapstructure:"versions"` + ActiveVersion *int `mapstructure:"version"` + Comment *string `mapstructure:"comment"` + CreatedAt *time.Time `mapstructure:"created_at"` + CustomerID *string `mapstructure:"customer_id"` + DeletedAt *time.Time `mapstructure:"deleted_at"` + ServiceID *string `mapstructure:"id"` + Name *string `mapstructure:"name"` + Type *string `mapstructure:"type"` + UpdatedAt *time.Time `mapstructure:"updated_at"` + Versions []*Version `mapstructure:"versions"` + Environments []*Environment `mapstructure:"environments"` } // ServiceDetail represents a server response from the Fastly API. type ServiceDetail struct { - ActiveVersion *Version `mapstructure:"active_version"` - Comment *string `mapstructure:"comment"` - CreatedAt *time.Time `mapstructure:"created_at"` - CustomerID *string `mapstructure:"customer_id"` - DeletedAt *time.Time `mapstructure:"deleted_at"` - ServiceID *string `mapstructure:"id"` - Name *string `mapstructure:"name"` - Type *string `mapstructure:"type"` - UpdatedAt *time.Time `mapstructure:"updated_at"` - Version *Version `mapstructure:"version"` - Versions []*Version `mapstructure:"versions"` + ActiveVersion *Version `mapstructure:"active_version"` + Comment *string `mapstructure:"comment"` + CreatedAt *time.Time `mapstructure:"created_at"` + CustomerID *string `mapstructure:"customer_id"` + DeletedAt *time.Time `mapstructure:"deleted_at"` + ServiceID *string `mapstructure:"id"` + Name *string `mapstructure:"name"` + Type *string `mapstructure:"type"` + UpdatedAt *time.Time `mapstructure:"updated_at"` + Version *Version `mapstructure:"version"` + Versions []*Version `mapstructure:"versions"` + Environments []*Environment `mapstructure:"environments"` } // ServiceDomain represents a server response from the Fastly API. diff --git a/fastly/service_version.go b/fastly/service_version.go index 4ebf20440..817668de5 100644 --- a/fastly/service_version.go +++ b/fastly/service_version.go @@ -7,17 +7,25 @@ import ( // Version represents a distinct configuration version. type Version struct { - Active *bool `mapstructure:"active"` - Comment *string `mapstructure:"comment"` - CreatedAt *time.Time `mapstructure:"created_at"` - DeletedAt *time.Time `mapstructure:"deleted_at"` - Deployed *bool `mapstructure:"deployed"` - Locked *bool `mapstructure:"locked"` - Number *int `mapstructure:"number"` - ServiceID *string `mapstructure:"service_id"` - Staging *bool `mapstructure:"staging"` - Testing *bool `mapstructure:"testing"` - UpdatedAt *time.Time `mapstructure:"updated_at"` + Active *bool `mapstructure:"active"` + Comment *string `mapstructure:"comment"` + CreatedAt *time.Time `mapstructure:"created_at"` + DeletedAt *time.Time `mapstructure:"deleted_at"` + Deployed *bool `mapstructure:"deployed"` + Locked *bool `mapstructure:"locked"` + Number *int `mapstructure:"number"` + ServiceID *string `mapstructure:"service_id"` + Staging *bool `mapstructure:"staging"` + Testing *bool `mapstructure:"testing"` + UpdatedAt *time.Time `mapstructure:"updated_at"` + Environments []*Environment `mapstructure:"environments"` +} + +// Environment represents a distinct deployment environment. +type Environment struct { + ServiceVersion *int64 `mapstructure:"active_version"` + Name *string `mapstructure:"name"` + ServiceID *string `mapstructure:"service_id"` } // ListVersionsInput is the input to the ListVersions function. @@ -176,6 +184,8 @@ type ActivateVersionInput struct { ServiceID string // ServiceVersion is the specific configuration version (required). ServiceVersion int + // Environment is the Fastly environment to activate this version to (optional). + Environment string } // ActivateVersion activates the given version. @@ -187,7 +197,13 @@ func (c *Client) ActivateVersion(i *ActivateVersionInput) (*Version, error) { return nil, ErrMissingServiceVersion } - path := ToSafeURL("service", i.ServiceID, "version", strconv.Itoa(i.ServiceVersion), "activate") + components := []string{"service", i.ServiceID, "version", strconv.Itoa(i.ServiceVersion), "activate"} + if i.Environment != "" { + components = append(components, i.Environment) + } + + path := ToSafeURL(components...) + resp, err := c.Put(path, nil) if err != nil { return nil, err @@ -207,6 +223,8 @@ type DeactivateVersionInput struct { ServiceID string // ServiceVersion is the specific configuration version (required). ServiceVersion int + // Environment is the Fastly environment to deactivate this version from (optional). + Environment string } // DeactivateVersion deactivates the given version. @@ -218,7 +236,13 @@ func (c *Client) DeactivateVersion(i *DeactivateVersionInput) (*Version, error) return nil, ErrMissingServiceVersion } - path := ToSafeURL("service", i.ServiceID, "version", strconv.Itoa(i.ServiceVersion), "deactivate") + components := []string{"service", i.ServiceID, "version", strconv.Itoa(i.ServiceVersion), "deactivate"} + if i.Environment != "" { + components = append(components, i.Environment) + } + + path := ToSafeURL(components...) + resp, err := c.Put(path, nil) if err != nil { return nil, err