Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Add codefresh_pipeline Data Source #110

Merged
merged 5 commits into from
Mar 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)