Skip to content

Commit

Permalink
Datasource version matching the resource definition
Browse files Browse the repository at this point in the history
  • Loading branch information
sandrogattuso committed Mar 10, 2021
1 parent 0b25158 commit 84b9f2d
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 113 deletions.
71 changes: 39 additions & 32 deletions codefresh/data_step_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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,
},
},
},
},
},
}
Expand All @@ -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
}
45 changes: 0 additions & 45 deletions codefresh/data_step_types_versions.go

This file was deleted.

17 changes: 8 additions & 9 deletions codefresh/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
23 changes: 0 additions & 23 deletions docs/data/step-types-versions.md

This file was deleted.

17 changes: 13 additions & 4 deletions docs/data/step-types.md
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -8,18 +8,27 @@ 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
}
```

## 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

0 comments on commit 84b9f2d

Please sign in to comment.