From cb621e7dc4fc32d46ce6e9a1d28e18e68268c52d Mon Sep 17 00:00:00 2001 From: clairehuizenga Date: Mon, 24 Nov 2025 15:14:57 +1100 Subject: [PATCH] add new resource and service --- .../platform_hub_git_credential.go | 86 +++++++++++++++++ .../platform_hub_git_credential_service.go | 93 +++++++++++++++++++ 2 files changed, 179 insertions(+) create mode 100644 pkg/platformhubgitcredential/platform_hub_git_credential.go create mode 100644 pkg/platformhubgitcredential/platform_hub_git_credential_service.go diff --git a/pkg/platformhubgitcredential/platform_hub_git_credential.go b/pkg/platformhubgitcredential/platform_hub_git_credential.go new file mode 100644 index 00000000..f35dfc45 --- /dev/null +++ b/pkg/platformhubgitcredential/platform_hub_git_credential.go @@ -0,0 +1,86 @@ +package platformhubgitcredential + +import ( + "encoding/json" + "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/credentials" + "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/resources" +) + +// PlatformHubGitCredential represents a Platform Hub git credential resource. +type PlatformHubGitCredential struct { + Description string `json:"Description,omitempty"` + Details credentials.GitCredential `json:"Details"` + Name string `json:"Name"` + RepositoryRestrictions *credentials.RepositoryRestrictions `json:"RepositoryRestrictions"` + resources.Resource +} + +// NewPlatformHubGitCredential creates and initializes a Platform Hub git credential resource. +func NewPlatformHubGitCredential(name string, credential credentials.GitCredential) *PlatformHubGitCredential { + return &PlatformHubGitCredential{ + Details: credential, + Name: name, + Resource: *resources.NewResource(), + } +} + +// GetName returns the name of the resource. +func (r *PlatformHubGitCredential) GetName() string { + return r.Name +} + +// SetName sets the name of the resource. +func (r *PlatformHubGitCredential) SetName(name string) { + r.Name = name +} + +// UnmarshalJSON sets the resource to its representation in JSON. +func (r *PlatformHubGitCredential) UnmarshalJSON(b []byte) error { + var fields struct { + Description string `json:"Description,omitempty"` + Name string `json:"Name"` + resources.Resource + } + if err := json.Unmarshal(b, &fields); err != nil { + return err + } + + r.Description = fields.Description + r.Name = fields.Name + r.Resource = fields.Resource + + var rawResource map[string]*json.RawMessage + if err := json.Unmarshal(b, &rawResource); err != nil { + return err + } + + var repositoryRestrictions credentials.RepositoryRestrictions + if rawResource["RepositoryRestrictions"] != nil { + restrictionsValue := rawResource["RepositoryRestrictions"] + + if err := json.Unmarshal(*restrictionsValue, &repositoryRestrictions); err != nil { + return err + } + r.RepositoryRestrictions = &repositoryRestrictions + } + + var gitCredentials *json.RawMessage + + if rawResource["Details"] != nil { + detailsValue := rawResource["Details"] + + if err := json.Unmarshal(*detailsValue, &gitCredentials); err != nil { + return err + } + + var usernamePasswordGitCredential *credentials.UsernamePassword + if err := json.Unmarshal(*gitCredentials, &usernamePasswordGitCredential); err != nil { + return err + } + r.Details = usernamePasswordGitCredential + } + + return nil +} + +var _ resources.IHasName = &PlatformHubGitCredential{} diff --git a/pkg/platformhubgitcredential/platform_hub_git_credential_service.go b/pkg/platformhubgitcredential/platform_hub_git_credential_service.go new file mode 100644 index 00000000..0c3e2ed8 --- /dev/null +++ b/pkg/platformhubgitcredential/platform_hub_git_credential_service.go @@ -0,0 +1,93 @@ +package platformhubgitcredential + +import ( + "github.com/OctopusDeploy/go-octopusdeploy/v2/internal" + "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/newclient" +) + +const template = "/api/platformhub/git-credentials{/id}" + +// Add creates a new Platform Hub git credential. +func Add(client newclient.Client, platformHubGitCredential *PlatformHubGitCredential) (*PlatformHubGitCredential, error) { + if platformHubGitCredential == nil { + return nil, internal.CreateRequiredParameterIsEmptyOrNilError("platformHubGitCredential") + } + + if platformHubGitCredential.Name == "" { + return nil, internal.CreateRequiredParameterIsEmptyOrNilError("platformHubGitCredential.Name") + } + + path, err := client.URITemplateCache().Expand(template, map[string]any{}) + if err != nil { + return nil, err + } + + res, err := newclient.Post[PlatformHubGitCredential](client.HttpSession(), path, platformHubGitCredential) + if err != nil { + return nil, err + } + return res, nil +} + +// GetByID returns the Platform Hub git credential that matches the input ID. +func GetByID(client newclient.Client, id string) (*PlatformHubGitCredential, error) { + if id == "" { + return nil, internal.CreateRequiredParameterIsEmptyOrNilError("id") + } + + path, err := client.URITemplateCache().Expand(template, map[string]any{"id": id}) + if err != nil { + return nil, err + } + + res, err := newclient.Get[PlatformHubGitCredential](client.HttpSession(), path) + if err != nil { + return nil, err + } + + return res, nil +} + +// Update modifies a Platform Hub git credential. +func Update(client newclient.Client, platformHubGitCredential *PlatformHubGitCredential) (*PlatformHubGitCredential, error) { + if platformHubGitCredential == nil { + return nil, internal.CreateRequiredParameterIsEmptyOrNilError("platformHubGitCredential") + } + + if platformHubGitCredential.ID == "" { + return nil, internal.CreateRequiredParameterIsEmptyOrNilError("platformHubGitCredential.ID") + } + + if platformHubGitCredential.Name == "" { + return nil, internal.CreateRequiredParameterIsEmptyOrNilError("platformHubGitCredential.Name") + } + + path, err := client.URITemplateCache().Expand(template, map[string]any{"id": platformHubGitCredential.ID}) + if err != nil { + return nil, err + } + + res, err := newclient.Put[PlatformHubGitCredential](client.HttpSession(), path, platformHubGitCredential) + if err != nil { + return nil, err + } + return res, nil +} + +// Delete removes a Platform Hub git credential with the specified ID. +func Delete(client newclient.Client, id string) error { + if id == "" { + return internal.CreateRequiredParameterIsEmptyOrNilError("id") + } + + path, err := client.URITemplateCache().Expand(template, map[string]any{"id": id}) + if err != nil { + return err + } + + err = newclient.Delete(client.HttpSession(), path) + if err != nil { + return err + } + return nil +}