diff --git a/client/pipeline.go b/client/pipeline.go index f1d44656..0d505583 100644 --- a/client/pipeline.go +++ b/client/pipeline.go @@ -6,6 +6,11 @@ import ( "strings" ) +type Pipelines struct { + Docs []Pipeline `json:"docs,omitempty"` + Count int `json:"count,omitempty"` +} + type ErrorResponse struct { Status int `json:"status,omitempty"` Message string `json:"message,omitempty"` @@ -179,6 +184,29 @@ func (client *Client) GetPipeline(name string) (*Pipeline, error) { return &pipeline, nil } +func (client *Client) GetPipelines() (*[]Pipeline, error) { + fullPath := "/pipelines" + opts := RequestOptions{ + Path: fullPath, + Method: "GET", + } + + resp, err := client.RequestAPI(&opts) + + if err != nil { + return nil, err + } + + var getPipelines Pipelines + + err = DecodeResponseInto(resp, &getPipelines) + if err != nil { + return nil, err + } + + return &getPipelines.Docs, nil +} + func (client *Client) CreatePipeline(pipeline *Pipeline) (*Pipeline, error) { body, err := EncodeToJSON(pipeline) diff --git a/codefresh/data_pipelines.go b/codefresh/data_pipelines.go new file mode 100644 index 00000000..681489a5 --- /dev/null +++ b/codefresh/data_pipelines.go @@ -0,0 +1,119 @@ +package codefresh + +import ( + "fmt" + "regexp" + "time" + + cfClient "github.com/codefresh-io/terraform-provider-codefresh/client" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +func dataSourcePipelines() *schema.Resource { + return &schema.Resource{ + Description: "This resource retrives all pipelines belonging to the current user, which can be optionally filtered by the name.", + Read: dataSourcePipelinesRead, + Schema: map[string]*schema.Schema{ + "name_regex": { + Description: "The name regular expression to filter pipelines by.", + Type: schema.TypeString, + Optional: true, + }, + "pipelines": { + Description: "The returned list of pipelines. Note that `spec` is currently limited to the YAML, because of the complexity of the object.", + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "id": { + Type: schema.TypeString, + Computed: true, + }, + "name": { + Type: schema.TypeString, + Computed: true, + }, + "project": { + Type: schema.TypeString, + Computed: true, + }, + "tags": { + Type: schema.TypeList, + Computed: true, + Elem: schema.TypeString, + }, + "is_public": { + Type: schema.TypeBool, + Computed: true, + }, + "spec": { + Type: schema.TypeString, + Computed: true, + }, + }, + }, + }, + }, + } +} + +func dataSourcePipelinesRead(d *schema.ResourceData, meta interface{}) error { + + client := meta.(*cfClient.Client) + + pipelines, err := client.GetPipelines() + if err != nil { + return err + } + + err = mapDataPipelinesToResource(*pipelines, d) + if err != nil { + return err + } + + d.SetId(time.Now().UTC().String()) + + return nil +} + +func mapDataPipelinesToResource(pipelines []cfClient.Pipeline, d *schema.ResourceData) error { + var res = make([]map[string]interface{}, len(pipelines)) + for i, p := range pipelines { + m := make(map[string]interface{}) + m["id"] = p.Metadata.ID + m["name"] = p.Metadata.Name + m["project"] = p.Metadata.Project + m["tags"] = p.Metadata.Labels.Tags + m["is_public"] = p.Metadata.IsPublic + m["spec"] = p.Metadata.OriginalYamlString + + res[i] = m + } + + filteredPipelines := make([]map[string]interface{}, 0) + for _, p := range res { + match := false + + name, ok := d.GetOk("name_regex") + if !ok { + match = true + } else { + r, err := regexp.Compile(name.(string)) + if err != nil { + return fmt.Errorf("`name_regex` is not a valid regular expression, %s", err.Error()) + } + match = r.MatchString(p["name"].(string)) + } + + if match { + filteredPipelines = append(filteredPipelines, p) + } + } + + err := d.Set("pipelines", filteredPipelines) + if err != nil { + return err + } + + return nil +} diff --git a/codefresh/data_users.go b/codefresh/data_users.go index 5495d6bd..43cd89d1 100644 --- a/codefresh/data_users.go +++ b/codefresh/data_users.go @@ -33,6 +33,9 @@ func dataSourceUsersRead(d *schema.ResourceData, meta interface{}) error { } err = mapDataUsersToResource(*users, d) + if err != nil { + return err + } d.SetId(time.Now().UTC().String()) diff --git a/codefresh/provider.go b/codefresh/provider.go index 4e90198f..fba23292 100644 --- a/codefresh/provider.go +++ b/codefresh/provider.go @@ -39,6 +39,7 @@ func Provider() *schema.Provider { "codefresh_user": dataSourceUser(), "codefresh_users": dataSourceUsers(), "codefresh_registry": dataSourceRegistry(), + "codefresh_pipelines": dataSourcePipelines(), }, ResourcesMap: map[string]*schema.Resource{ "codefresh_account": resourceAccount(), diff --git a/docs/data-sources/pipelines.md b/docs/data-sources/pipelines.md new file mode 100644 index 00000000..a60dadc3 --- /dev/null +++ b/docs/data-sources/pipelines.md @@ -0,0 +1,39 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "codefresh_pipelines Data Source - terraform-provider-codefresh" +subcategory: "" +description: |- + This resource retrives all pipelines belonging to the current user, which can be optionally filtered by the name. +--- + +# codefresh_pipelines (Data Source) + +This resource retrives all pipelines belonging to the current user, which can be optionally filtered by the name. + + + + +## Schema + +### Optional + +- `name_regex` (String) The name regular expression to filter pipelines by. + +### Read-Only + +- `id` (String) The ID of this resource. +- `pipelines` (List of Object) The returned list of pipelines. Note that `spec` is currently limited to the YAML, because of the complexity of the object. (see [below for nested schema](#nestedatt--pipelines)) + + +### Nested Schema for `pipelines` + +Read-Only: + +- `id` (String) +- `is_public` (Boolean) +- `name` (String) +- `project` (String) +- `spec` (String) +- `tags` (List of String) + +