Skip to content

Commit bd3ca36

Browse files
authored
Feat: Add codefresh_pipeline Data Source (#110)
## 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]>
1 parent 0d4a54f commit bd3ca36

File tree

5 files changed

+190
-0
lines changed

5 files changed

+190
-0
lines changed

client/pipeline.go

+28
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ import (
66
"strings"
77
)
88

9+
type Pipelines struct {
10+
Docs []Pipeline `json:"docs,omitempty"`
11+
Count int `json:"count,omitempty"`
12+
}
13+
914
type ErrorResponse struct {
1015
Status int `json:"status,omitempty"`
1116
Message string `json:"message,omitempty"`
@@ -179,6 +184,29 @@ func (client *Client) GetPipeline(name string) (*Pipeline, error) {
179184
return &pipeline, nil
180185
}
181186

187+
func (client *Client) GetPipelines() (*[]Pipeline, error) {
188+
fullPath := "/pipelines"
189+
opts := RequestOptions{
190+
Path: fullPath,
191+
Method: "GET",
192+
}
193+
194+
resp, err := client.RequestAPI(&opts)
195+
196+
if err != nil {
197+
return nil, err
198+
}
199+
200+
var getPipelines Pipelines
201+
202+
err = DecodeResponseInto(resp, &getPipelines)
203+
if err != nil {
204+
return nil, err
205+
}
206+
207+
return &getPipelines.Docs, nil
208+
}
209+
182210
func (client *Client) CreatePipeline(pipeline *Pipeline) (*Pipeline, error) {
183211

184212
body, err := EncodeToJSON(pipeline)

codefresh/data_pipelines.go

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package codefresh
2+
3+
import (
4+
"fmt"
5+
"regexp"
6+
"time"
7+
8+
cfClient "github.com/codefresh-io/terraform-provider-codefresh/client"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10+
)
11+
12+
func dataSourcePipelines() *schema.Resource {
13+
return &schema.Resource{
14+
Description: "This resource retrives all pipelines belonging to the current user, which can be optionally filtered by the name.",
15+
Read: dataSourcePipelinesRead,
16+
Schema: map[string]*schema.Schema{
17+
"name_regex": {
18+
Description: "The name regular expression to filter pipelines by.",
19+
Type: schema.TypeString,
20+
Optional: true,
21+
},
22+
"pipelines": {
23+
Description: "The returned list of pipelines. Note that `spec` is currently limited to the YAML, because of the complexity of the object.",
24+
Type: schema.TypeList,
25+
Computed: true,
26+
Elem: &schema.Resource{
27+
Schema: map[string]*schema.Schema{
28+
"id": {
29+
Type: schema.TypeString,
30+
Computed: true,
31+
},
32+
"name": {
33+
Type: schema.TypeString,
34+
Computed: true,
35+
},
36+
"project": {
37+
Type: schema.TypeString,
38+
Computed: true,
39+
},
40+
"tags": {
41+
Type: schema.TypeList,
42+
Computed: true,
43+
Elem: schema.TypeString,
44+
},
45+
"is_public": {
46+
Type: schema.TypeBool,
47+
Computed: true,
48+
},
49+
"spec": {
50+
Type: schema.TypeString,
51+
Computed: true,
52+
},
53+
},
54+
},
55+
},
56+
},
57+
}
58+
}
59+
60+
func dataSourcePipelinesRead(d *schema.ResourceData, meta interface{}) error {
61+
62+
client := meta.(*cfClient.Client)
63+
64+
pipelines, err := client.GetPipelines()
65+
if err != nil {
66+
return err
67+
}
68+
69+
err = mapDataPipelinesToResource(*pipelines, d)
70+
if err != nil {
71+
return err
72+
}
73+
74+
d.SetId(time.Now().UTC().String())
75+
76+
return nil
77+
}
78+
79+
func mapDataPipelinesToResource(pipelines []cfClient.Pipeline, d *schema.ResourceData) error {
80+
var res = make([]map[string]interface{}, len(pipelines))
81+
for i, p := range pipelines {
82+
m := make(map[string]interface{})
83+
m["id"] = p.Metadata.ID
84+
m["name"] = p.Metadata.Name
85+
m["project"] = p.Metadata.Project
86+
m["tags"] = p.Metadata.Labels.Tags
87+
m["is_public"] = p.Metadata.IsPublic
88+
m["spec"] = p.Metadata.OriginalYamlString
89+
90+
res[i] = m
91+
}
92+
93+
filteredPipelines := make([]map[string]interface{}, 0)
94+
for _, p := range res {
95+
match := false
96+
97+
name, ok := d.GetOk("name_regex")
98+
if !ok {
99+
match = true
100+
} else {
101+
r, err := regexp.Compile(name.(string))
102+
if err != nil {
103+
return fmt.Errorf("`name_regex` is not a valid regular expression, %s", err.Error())
104+
}
105+
match = r.MatchString(p["name"].(string))
106+
}
107+
108+
if match {
109+
filteredPipelines = append(filteredPipelines, p)
110+
}
111+
}
112+
113+
err := d.Set("pipelines", filteredPipelines)
114+
if err != nil {
115+
return err
116+
}
117+
118+
return nil
119+
}

codefresh/data_users.go

+3
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ func dataSourceUsersRead(d *schema.ResourceData, meta interface{}) error {
3333
}
3434

3535
err = mapDataUsersToResource(*users, d)
36+
if err != nil {
37+
return err
38+
}
3639

3740
d.SetId(time.Now().UTC().String())
3841

codefresh/provider.go

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ func Provider() *schema.Provider {
3939
"codefresh_user": dataSourceUser(),
4040
"codefresh_users": dataSourceUsers(),
4141
"codefresh_registry": dataSourceRegistry(),
42+
"codefresh_pipelines": dataSourcePipelines(),
4243
},
4344
ResourcesMap: map[string]*schema.Resource{
4445
"codefresh_account": resourceAccount(),

docs/data-sources/pipelines.md

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "codefresh_pipelines Data Source - terraform-provider-codefresh"
4+
subcategory: ""
5+
description: |-
6+
This resource retrives all pipelines belonging to the current user, which can be optionally filtered by the name.
7+
---
8+
9+
# codefresh_pipelines (Data Source)
10+
11+
This resource retrives all pipelines belonging to the current user, which can be optionally filtered by the name.
12+
13+
14+
15+
<!-- schema generated by tfplugindocs -->
16+
## Schema
17+
18+
### Optional
19+
20+
- `name_regex` (String) The name regular expression to filter pipelines by.
21+
22+
### Read-Only
23+
24+
- `id` (String) The ID of this resource.
25+
- `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))
26+
27+
<a id="nestedatt--pipelines"></a>
28+
### Nested Schema for `pipelines`
29+
30+
Read-Only:
31+
32+
- `id` (String)
33+
- `is_public` (Boolean)
34+
- `name` (String)
35+
- `project` (String)
36+
- `spec` (String)
37+
- `tags` (List of String)
38+
39+

0 commit comments

Comments
 (0)