Skip to content

Commit

Permalink
Feat: Add codefresh_pipeline Data Source (#110)
Browse files Browse the repository at this point in the history
## What

* Add `codefresh_pipeline` Data Source

## Why

* A way to retrieve and filter Pipelines is useful

## Notes

#87

## Checklist

* [x] _I have read
[CONTRIBUTING.md](https://github.com/codefresh-io/terraform-provider-codefresh/blob/master/README.md)._
* [x] _I have [allowed changes to my fork to be
made](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/allowing-changes-to-a-pull-request-branch-created-from-a-fork)._
* [x] _I have added tests, assuming new tests are warranted_.
* [x] _I understand that the `/test` comment will be ignored by the CI
trigger [unless it is made by a repo admin or
collaborator](https://codefresh.io/docs/docs/pipelines/triggers/git-triggers/#support-for-building-pull-requests-from-forks)._

---------

Co-authored-by: Yonatan Koren <[email protected]>
  • Loading branch information
korenyoni and korenyoni authored Mar 12, 2023
1 parent 0d4a54f commit bd3ca36
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 0 deletions.
28 changes: 28 additions & 0 deletions client/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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)
Expand Down
119 changes: 119 additions & 0 deletions codefresh/data_pipelines.go
Original file line number Diff line number Diff line change
@@ -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
}
3 changes: 3 additions & 0 deletions codefresh/data_users.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down
1 change: 1 addition & 0 deletions codefresh/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
39 changes: 39 additions & 0 deletions docs/data-sources/pipelines.md
Original file line number Diff line number Diff line change
@@ -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 generated by tfplugindocs -->
## 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))

<a id="nestedatt--pipelines"></a>
### Nested Schema for `pipelines`

Read-Only:

- `id` (String)
- `is_public` (Boolean)
- `name` (String)
- `project` (String)
- `spec` (String)
- `tags` (List of String)


0 comments on commit bd3ca36

Please sign in to comment.