Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
86 changes: 86 additions & 0 deletions pkg/platformhubgitcredential/platform_hub_git_credential.go
Original file line number Diff line number Diff line change
@@ -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{}
Original file line number Diff line number Diff line change
@@ -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
}
Loading