-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from sandrogattuso/feature/step-types
Introduce support for step-types
- Loading branch information
Showing
13 changed files
with
1,082 additions
and
11 deletions.
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,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 | ||
} |
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,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 | ||
} |
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.