Skip to content

Commit

Permalink
Introduce support for step-types
Browse files Browse the repository at this point in the history
  • Loading branch information
sandrogattuso committed Feb 21, 2021
1 parent ae3f213 commit e2c1b7d
Show file tree
Hide file tree
Showing 8 changed files with 502 additions and 13 deletions.
137 changes: 137 additions & 0 deletions client/step_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package client

import (
"fmt"
"log"
"net/url"
)

type StepTypes struct {
Version string `json:"version,omitempty"`
Kind string `json:"kind,omitempty"`
Metadata map[string]interface{} `json:"metadata,omitempty"`
Spec map[string]interface{} `json:"spec,omitempty"`
}

func (stepTypes *StepTypes) GetID() string {
return stepTypes.Metadata["name"].(string)
}

func (client *Client) GetStepTypesVersions(name string) ([]string, error) {
fullPath := fmt.Sprintf("/step-types/%s/versions", url.PathEscape(name))
opts := RequestOptions{
Path: fullPath,
Method: "GET",
}

resp, err := client.RequestAPI(&opts)

if err != nil {
return nil, err
}
var respStepTypesVersions []string
err = DecodeResponseInto(resp, &respStepTypesVersions)
if err != nil {
return nil, err
}
return respStepTypesVersions, nil
}

func (client *Client) GetStepTypes(identifier string) (*StepTypes, error) {
fullPath := fmt.Sprintf("/step-types/%s", url.PathEscape(identifier))
opts := RequestOptions{
Path: fullPath,
Method: "GET",
}

resp, err := client.RequestAPI(&opts)

if err != nil {
return nil, err
}
var respStepTypes StepTypes
err = DecodeResponseInto(resp, &respStepTypes)
if err != nil {
return nil, err
}

return &respStepTypes, nil

}

func (client *Client) CreateStepTypes(stepTypes *StepTypes) (*StepTypes, error) {

body, err := EncodeToJSON(stepTypes)

if err != nil {
return nil, err
}
opts := RequestOptions{
Path: "/step-types",
Method: "POST",
Body: body,
}

resp, err := client.RequestAPI(&opts)
if err != nil {
return nil, err
}
log.Printf("[DEBUG] Response step types: %q", resp)
var respStepTypes StepTypes
err = DecodeResponseInto(resp, &respStepTypes)
if err != nil {
log.Printf("[DEBUG] Error while decoding step types. Error = %v, Response: %q", err, respStepTypes)
return nil, err
}
log.Printf("[DEBUG] Decoded step types response: %q", respStepTypes.Metadata["name"])
return &respStepTypes, nil

}

func (client *Client) UpdateStepTypes(stepTypes *StepTypes) (*StepTypes, error) {

body, err := EncodeToJSON(stepTypes)

if err != nil {
return nil, err
}

fullPath := fmt.Sprintf("/step-types/%s", url.PathEscape(stepTypes.GetID()+":"+stepTypes.Metadata["version"].(string)))
opts := RequestOptions{
Path: fullPath,
Method: "PUT",
Body: body,
}

resp, err := client.RequestAPI(&opts)

if err != nil {
return nil, err
}

var respStepTypes StepTypes
err = DecodeResponseInto(resp, &respStepTypes)
if err != nil {
return nil, err
}

return &respStepTypes, nil

}

func (client *Client) DeleteStepTypes(name string) error {

fullPath := fmt.Sprintf("/step-types/%s", url.PathEscape(name))
opts := RequestOptions{
Path: fullPath,
Method: "DELETE",
}

_, err := client.RequestAPI(&opts)

if err != nil {
return err
}

return nil
}
65 changes: 65 additions & 0 deletions codefresh/data_step_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package codefresh

import (
"fmt"

cfClient "github.com/codefresh-io/terraform-provider-codefresh/client"
"github.com/ghodss/yaml"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourceStepTypes() *schema.Resource {
return &schema.Resource{
Read: dataSourceStepTypesRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"step_types_yaml": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func dataSourceStepTypesRead(d *schema.ResourceData, meta interface{}) error {

client := meta.(*cfClient.Client)
var stepTypes *cfClient.StepTypes
var err error

if name, nameOk := d.GetOk("name"); nameOk {
stepTypes, err = client.GetStepTypes(name.(string))
} else {
return fmt.Errorf("data.codefresh_step_types - must specify name")
}
if err != nil {
return err
}

if stepTypes == nil {
return fmt.Errorf("data.codefresh_step_types - cannot find step-types")
}

return mapDataSetTypesToResource(stepTypes, d)
}

func mapDataSetTypesToResource(stepTypes *cfClient.StepTypes, d *schema.ResourceData) error {

if stepTypes == nil || stepTypes.Metadata["name"].(string) == "" {
return fmt.Errorf("data.codefresh_step_types - failed to mapDataSetTypesToResource")
}
d.SetId(stepTypes.Metadata["name"].(string))

d.Set("name", d.Id())

stepTypesYaml, err := yaml.Marshal(stepTypes)
if err != nil {
return err
}
d.Set("step_types_yaml", string(stepTypesYaml))

return nil
}
45 changes: 45 additions & 0 deletions codefresh/data_step_types_versions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package codefresh

import (
"fmt"

cfClient "github.com/codefresh-io/terraform-provider-codefresh/client"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourceStepTypesVersions() *schema.Resource {
return &schema.Resource{
Read: dataSourceStepTypesVersionsRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"versions": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
}
}

func dataSourceStepTypesVersionsRead(d *schema.ResourceData, meta interface{}) error {

client := meta.(*cfClient.Client)
var versions []string
var err error

if name, nameOk := d.GetOk("name"); nameOk {
if versions, err = client.GetStepTypesVersions(name.(string)); err == nil {
d.SetId(name.(string))
d.Set("name", name.(string))
d.Set("versions", versions)
}
return err
}
return fmt.Errorf("data.codefresh_step_types_versions - must specify name")

}
29 changes: 16 additions & 13 deletions codefresh/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,28 @@ func Provider() *schema.Provider {
},
},
DataSourcesMap: map[string]*schema.Resource{
"codefresh_users": dataSourceUsers(),
"codefresh_user": dataSourceUser(),
"codefresh_idps": dataSourceIdps(),
"codefresh_account": dataSourceAccount(),
"codefresh_team": dataSourceTeam(),
"codefresh_current_account": dataSourceCurrentAccount(),
"codefresh_context": dataSourceContext(),
"codefresh_account": dataSourceAccount(),
"codefresh_context": dataSourceContext(),
"codefresh_current_account": dataSourceCurrentAccount(),
"codefresh_idps": dataSourceIdps(),
"codefresh_step_types": dataSourceStepTypes(),
"codefresh_step_types_versions": dataSourceStepTypesVersions(),
"codefresh_team": dataSourceTeam(),
"codefresh_user": dataSourceUser(),
"codefresh_users": dataSourceUsers(),
},
ResourcesMap: map[string]*schema.Resource{
"codefresh_project": resourceProject(),
"codefresh_pipeline": resourcePipeline(),
"codefresh_context": resourceContext(),
"codefresh_team": resourceTeam(),
"codefresh_account": resourceAccount(),
"codefresh_account_admins": resourceAccountAdmins(),
"codefresh_api_key": resourceApiKey(),
"codefresh_context": resourceContext(),
"codefresh_idp_accounts": resourceIDPAccounts(),
"codefresh_account_admins": resourceAccountAdmins(),
"codefresh_user": resourceUser(),
"codefresh_permission": resourcePermission(),
"codefresh_pipeline": resourcePipeline(),
"codefresh_project": resourceProject(),
"codefresh_step_types": resourceStepTypes(),
"codefresh_user": resourceUser(),
"codefresh_team": resourceTeam(),
},
ConfigureFunc: configureProvider,
}
Expand Down
Loading

0 comments on commit e2c1b7d

Please sign in to comment.