diff --git a/codefresh/data_step_types.go b/codefresh/data_step_types.go index 3a10020..928eefc 100644 --- a/codefresh/data_step_types.go +++ b/codefresh/data_step_types.go @@ -2,9 +2,9 @@ package codefresh import ( "fmt" + "log" cfClient "github.com/codefresh-io/terraform-provider-codefresh/client" - "github.com/ghodss/yaml" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" ) @@ -17,12 +17,20 @@ func dataSourceStepTypes() *schema.Resource { Required: true, }, "version": { - Type: schema.TypeString, - Optional: true, - }, - "step_types_yaml": { - Type: schema.TypeString, + 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, + }, + }, + }, }, }, } @@ -31,40 +39,39 @@ func dataSourceStepTypes() *schema.Resource { func dataSourceStepTypesRead(d *schema.ResourceData, meta interface{}) error { client := meta.(*cfClient.Client) - var stepTypes *cfClient.StepTypes var err error - identifier := d.Get("name").(string) - version, versionOk := d.GetOk("version") + var versions []string + stepTypesIdentifier := d.Get("name").(string) - if versionOk { - identifier = identifier + ":" + version.(string) - } - stepTypes, err = client.GetStepTypes(identifier) - if err != nil { - return err + 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) } - if stepTypes == nil { - return fmt.Errorf("data.codefresh_step_types - cannot find step-types") - } + return fmt.Errorf("data.codefresh_step_types - was unable to retrieve the versions for step_type %s", stepTypesIdentifier) - 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) +func mapDataSetTypesToResource(stepTypesVersions cfClient.StepTypesVersions, d *schema.ResourceData) error { + err := d.Set("name", stepTypesVersions.Name) if err != nil { return err } - d.Set("step_types_yaml", string(stepTypesYaml)) - - return nil + err = d.Set("version", flattenVersions(stepTypesVersions.Name, stepTypesVersions.Versions)) + return err } diff --git a/codefresh/data_step_types_versions.go b/codefresh/data_step_types_versions.go deleted file mode 100644 index 334517a..0000000 --- a/codefresh/data_step_types_versions.go +++ /dev/null @@ -1,45 +0,0 @@ -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") - -} diff --git a/codefresh/provider.go b/codefresh/provider.go index 4433018..bdf4182 100644 --- a/codefresh/provider.go +++ b/codefresh/provider.go @@ -27,15 +27,14 @@ func Provider() *schema.Provider { }, }, DataSourcesMap: map[string]*schema.Resource{ - "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(), + "codefresh_account": dataSourceAccount(), + "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_account": resourceAccount(), diff --git a/docs/data/step-types-versions.md b/docs/data/step-types-versions.md deleted file mode 100644 index 57150d1..0000000 --- a/docs/data/step-types-versions.md +++ /dev/null @@ -1,23 +0,0 @@ -# Data Source: codefresh_step_types_versions -This data source allows to retrieve the list of published versions of a step-types - -## Example Usage - -```hcl -data "codefresh_step_types_versions" "freestyle" { - name = "freestyle" -} - -output "versions" { - value = data.codefresh_step_types_versions.freestyle.versions -} - -``` - -## Argument Reference - -* `name` - (Required) Name of the step-types to be retrieved - -## Attributes Reference - -* `versions` - List of versions available for the custom plugin (step-types). diff --git a/docs/data/step-types.md b/docs/data/step-types.md index f14dae9..cedfe65 100644 --- a/docs/data/step-types.md +++ b/docs/data/step-types.md @@ -1,5 +1,5 @@ # Data Source: codefresh_step_types -This data source allows to retrieve the latest published version of a step-types +This data source allows to retrieve the published versions of a step-types ## Example Usage @@ -8,9 +8,13 @@ data "codefresh_step_types" "freestyle" { name = "freestyle" } +local { + freestyle_map = { for step_definition in data.codefresh_step_types.freestyle.version: step_definition.version_number => step_definition } +} + output "test" { # Value is return as YAML - value = yamldecode(data.codefresh_step_types.freestyle.step_types_yaml).metadata.updated_at + value = local.freestyle_map[keys(local.freestyle_map)[0]].version_number } ``` @@ -18,8 +22,13 @@ output "test" { ## Argument Reference * `name` - (Required) Name of the step-types to be retrieved -* `version` - (Optional) Version to be retrieved. If not specified, the latest published will be returned ## Attributes Reference -* `step_types_yaml` - The yaml string representing the custom plugin (step-types). +- `version` - A Set of `version` blocks as documented below. + +--- + +`version` provides the following: +- `version_number` - String representing the semVer for the step +- `step_types_yaml` - YAML String containing the definition of a typed plugin