-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: get/create/delete commands for agent types (#207)
- Loading branch information
Showing
9 changed files
with
403 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package client | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
models "github.com/semaphoreci/cli/api/models" | ||
) | ||
|
||
type AgentTypeApiV1AlphaApi struct { | ||
BaseClient BaseClient | ||
ResourceNameSingular string | ||
ResourceNamePlural string | ||
} | ||
|
||
func NewAgentTypeApiV1AlphaApi() AgentTypeApiV1AlphaApi { | ||
baseClient := NewBaseClientFromConfig() | ||
baseClient.SetApiVersion("v1alpha") | ||
|
||
return AgentTypeApiV1AlphaApi{ | ||
BaseClient: baseClient, | ||
ResourceNamePlural: "self_hosted_agent_types", | ||
ResourceNameSingular: "self_hosted_agent_type", | ||
} | ||
} | ||
|
||
func (c *AgentTypeApiV1AlphaApi) ListAgentTypes() (*models.AgentTypeListV1Alpha, error) { | ||
body, status, err := c.BaseClient.List(c.ResourceNamePlural) | ||
|
||
if err != nil { | ||
return nil, errors.New(fmt.Sprintf("connecting to Semaphore failed '%s'", err)) | ||
} | ||
|
||
if status != 200 { | ||
return nil, errors.New(fmt.Sprintf("http status %d with message \"%s\" received from upstream", status, body)) | ||
} | ||
|
||
return models.NewAgentTypeListV1AlphaFromJson(body) | ||
} | ||
|
||
func (c *AgentTypeApiV1AlphaApi) GetAgentType(name string) (*models.AgentTypeV1Alpha, error) { | ||
body, status, err := c.BaseClient.Get(c.ResourceNamePlural, name) | ||
|
||
if err != nil { | ||
return nil, errors.New(fmt.Sprintf("connecting to Semaphore failed '%s'", err)) | ||
} | ||
|
||
if status != 200 { | ||
return nil, errors.New(fmt.Sprintf("http status %d with message \"%s\" received from upstream", status, body)) | ||
} | ||
|
||
return models.NewAgentTypeV1AlphaFromJson(body) | ||
} | ||
|
||
func (c *AgentTypeApiV1AlphaApi) DeleteAgentType(name string) error { | ||
body, status, err := c.BaseClient.Delete(c.ResourceNamePlural, name) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
if status != 200 { | ||
return fmt.Errorf("http status %d with message \"%s\" received from upstream", status, body) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (c *AgentTypeApiV1AlphaApi) CreateAgentType(d *models.AgentTypeV1Alpha) (*models.AgentTypeV1Alpha, error) { | ||
json_body, err := d.ToJson() | ||
|
||
if err != nil { | ||
return nil, errors.New(fmt.Sprintf("failed to serialize object '%s'", err)) | ||
} | ||
|
||
body, status, err := c.BaseClient.Post(c.ResourceNamePlural, json_body) | ||
|
||
if err != nil { | ||
return nil, errors.New(fmt.Sprintf("creating %s on Semaphore failed '%s'", c.ResourceNameSingular, err)) | ||
} | ||
|
||
if status != 200 { | ||
return nil, errors.New(fmt.Sprintf("http status %d with message \"%s\" received from upstream", status, body)) | ||
} | ||
|
||
return models.NewAgentTypeV1AlphaFromJson(body) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package models | ||
|
||
import "encoding/json" | ||
|
||
type AgentTypeListV1Alpha struct { | ||
AgentTypes []AgentTypeV1Alpha `json:"agent_types" yaml:"agent_types"` | ||
} | ||
|
||
func NewAgentTypeListV1AlphaFromJson(data []byte) (*AgentTypeListV1Alpha, error) { | ||
list := AgentTypeListV1Alpha{} | ||
|
||
err := json.Unmarshal(data, &list) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, s := range list.AgentTypes { | ||
if s.ApiVersion == "" { | ||
s.ApiVersion = "v1alpha" | ||
} | ||
|
||
if s.Kind == "" { | ||
s.Kind = "SelfHostedAgentType" | ||
} | ||
} | ||
|
||
return &list, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package models | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
yaml "gopkg.in/yaml.v2" | ||
) | ||
|
||
type AgentTypeV1Alpha struct { | ||
ApiVersion string `json:"apiVersion,omitempty" yaml:"apiVersion"` | ||
Kind string `json:"kind,omitempty" yaml:"kind"` | ||
Metadata AgentTypeV1AlphaMetadata `json:"metadata" yaml:"metadata"` | ||
Status AgentTypeV1AlphaStatus `json:"status" yaml:"status"` | ||
} | ||
|
||
type AgentTypeV1AlphaMetadata struct { | ||
Name string `json:"name,omitempty" yaml:"name,omitempty"` | ||
CreateTime json.Number `json:"create_time,omitempty" yaml:"create_time,omitempty"` | ||
UpdateTime json.Number `json:"update_time,omitempty" yaml:"update_time,omitempty"` | ||
} | ||
|
||
type AgentTypeV1AlphaStatus struct { | ||
TotalAgentCount int `json:"total_agent_count,omitempty" yaml:"total_agent_count,omitempty"` | ||
RegistrationToken string `json:"registration_token,omitempty" yaml:"registration_token,omitempty"` | ||
} | ||
|
||
func NewAgentTypeV1Alpha(name string) AgentTypeV1Alpha { | ||
a := AgentTypeV1Alpha{} | ||
a.Metadata.Name = name | ||
a.setApiVersionAndKind() | ||
return a | ||
} | ||
|
||
func NewAgentTypeV1AlphaFromJson(data []byte) (*AgentTypeV1Alpha, error) { | ||
a := AgentTypeV1Alpha{} | ||
|
||
err := json.Unmarshal(data, &a) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
a.setApiVersionAndKind() | ||
return &a, nil | ||
} | ||
|
||
func NewAgentTypeV1AlphaFromYaml(data []byte) (*AgentTypeV1Alpha, error) { | ||
a := AgentTypeV1Alpha{} | ||
|
||
err := yaml.UnmarshalStrict(data, &a) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
a.setApiVersionAndKind() | ||
return &a, nil | ||
} | ||
|
||
func (s *AgentTypeV1Alpha) setApiVersionAndKind() { | ||
s.ApiVersion = "v1alpha" | ||
s.Kind = "SelfHostedAgentType" | ||
} | ||
|
||
func (s *AgentTypeV1Alpha) ObjectName() string { | ||
return fmt.Sprintf("SelfHostedAgentType/%s", s.Metadata.Name) | ||
} | ||
|
||
func (s *AgentTypeV1Alpha) ToJson() ([]byte, error) { | ||
return json.Marshal(s) | ||
} | ||
|
||
func (s *AgentTypeV1Alpha) ToYaml() ([]byte, error) { | ||
return yaml.Marshal(s) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.