Skip to content

Commit

Permalink
Merge pull request #33 from sandrogattuso/feature/step-types
Browse files Browse the repository at this point in the history
Introduce support for step-types
  • Loading branch information
vadimgusev-codefresh authored Mar 10, 2021
2 parents ae3f213 + 84b9f2d commit 7dcd8d1
Show file tree
Hide file tree
Showing 13 changed files with 1,082 additions and 11 deletions.
155 changes: 155 additions & 0 deletions client/step_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
package client

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

"github.com/iancoleman/orderedmap"
)

type StepTypesVersions struct {
Name string
Versions []StepTypesVersion
}
type StepTypesVersion struct {
VersionNumber string
StepTypes StepTypes
}

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

type SpecStepTypes struct {
Arguments string `json:"arguments,omitempty"`
Delimiters map[string]interface{} `json:"delimiters,omitempty"`
Returns string `json:"returns,omitempty"`
Steps *orderedmap.OrderedMap `json:"steps,omitempty"`
StepsTemplate string `json:"stepsTemplate,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
}

var respStepTypes StepTypes
err = DecodeResponseInto(resp, &respStepTypes)
if err != nil {
log.Printf("[DEBUG] Error while decoding step types. Error = %v, Response: %v", err, respStepTypes)
return nil, err
}
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.Metadata["name"].(string)+":"+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
}
77 changes: 77 additions & 0 deletions codefresh/data_step_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package codefresh

import (
"fmt"
"log"

cfClient "github.com/codefresh-io/terraform-provider-codefresh/client"
"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,
},
"version": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"version_number": {
Type: schema.TypeString,
Computed: true,
},
"step_types_yaml": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}

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

client := meta.(*cfClient.Client)
var err error
var versions []string
stepTypesIdentifier := d.Get("name").(string)

d.SetId(stepTypesIdentifier)
if versions, err = client.GetStepTypesVersions(stepTypesIdentifier); err == nil {
var stepVersions cfClient.StepTypesVersions
stepVersions.Name = stepTypesIdentifier
d.Set("versions", versions)
for _, version := range versions {
stepTypes, err := client.GetStepTypes(stepTypesIdentifier + ":" + version)
if err != nil {
log.Printf("[DEBUG] Skipping version %v due to error %v", version, err)
} else {
stepVersion := cfClient.StepTypesVersion{
VersionNumber: version,
StepTypes: *stepTypes,
}
stepVersions.Versions = append(stepVersions.Versions, stepVersion)
}
}
return mapStepTypesVersionsToResource(stepVersions, d)
}

return fmt.Errorf("data.codefresh_step_types - was unable to retrieve the versions for step_type %s", stepTypesIdentifier)

}

func mapDataSetTypesToResource(stepTypesVersions cfClient.StepTypesVersions, d *schema.ResourceData) error {
err := d.Set("name", stepTypesVersions.Name)
if err != nil {
return err
}
err = d.Set("version", flattenVersions(stepTypesVersions.Name, stepTypesVersions.Versions))
return err
}
24 changes: 13 additions & 11 deletions codefresh/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,27 @@ 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_current_account": dataSourceCurrentAccount(),
"codefresh_idps": dataSourceIdps(),
"codefresh_step_types": dataSourceStepTypes(),
"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 7dcd8d1

Please sign in to comment.