-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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]>
- Loading branch information
Showing
5 changed files
with
190 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
|