-
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.
- Loading branch information
1 parent
ae3f213
commit e2c1b7d
Showing
8 changed files
with
502 additions
and
13 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,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 | ||
} |
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,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 | ||
} |
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,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") | ||
|
||
} |
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.