Skip to content

Commit 353f7d9

Browse files
Jake-Mok-Nelsonkorenyoniilia-medvedev-codefresh
authored
Feat: Data source project (#117)
## What Create a project data resource for the Terraform Codefresh provider Fixes #116 ## Why A project data resource would allow us to perform validation before allowing a pipeline to target a project that may or may not exist. Ordinarily you'd just target the project resource but pipelines are not always created in the same location as the projects. ## Notes I have no ability to test this at the moment (I do not have connectivity to a Codefresh system), I'm hoping you can validate that it's good. ## 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)._ * [ ] _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]> Co-authored-by: Ilia Medvedev <[email protected]>
1 parent 87d6913 commit 353f7d9

File tree

4 files changed

+139
-0
lines changed

4 files changed

+139
-0
lines changed

codefresh/data_project.go

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package codefresh
2+
3+
import (
4+
"fmt"
5+
6+
cfClient "github.com/codefresh-io/terraform-provider-codefresh/codefresh/cfclient"
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8+
)
9+
10+
func dataSourceProject() *schema.Resource {
11+
return &schema.Resource{
12+
Description: "This data source retrieves a project by its ID or name.",
13+
Read: dataSourceProjectRead,
14+
Schema: map[string]*schema.Schema{
15+
"_id": {
16+
Type: schema.TypeString,
17+
Optional: true,
18+
},
19+
"name": {
20+
Type: schema.TypeString,
21+
Optional: true,
22+
},
23+
"tags": {
24+
Type: schema.TypeList,
25+
Optional: true,
26+
Elem: &schema.Schema{
27+
Type: schema.TypeString,
28+
},
29+
},
30+
},
31+
}
32+
}
33+
34+
func dataSourceProjectRead(d *schema.ResourceData, meta interface{}) error {
35+
36+
client := meta.(*cfClient.Client)
37+
var project *cfClient.Project
38+
var err error
39+
40+
if _id, _idOk := d.GetOk("_id"); _idOk {
41+
project, err = client.GetProjectByID(_id.(string))
42+
} else if name, nameOk := d.GetOk("name"); nameOk {
43+
project, err = client.GetProjectByName(name.(string))
44+
}
45+
46+
if err != nil {
47+
return err
48+
}
49+
50+
if project == nil {
51+
return fmt.Errorf("data.codefresh_project - cannot find project")
52+
}
53+
54+
return mapDataProjectToResource(project, d)
55+
56+
}
57+
58+
func mapDataProjectToResource(project *cfClient.Project, d *schema.ResourceData) error {
59+
60+
if project == nil || project.ID == "" {
61+
return fmt.Errorf("data.codefresh_project - failed to mapDataProjectToResource")
62+
}
63+
d.SetId(project.ID)
64+
65+
d.Set("_id", project.ID)
66+
d.Set("tags", project.Tags)
67+
68+
return nil
69+
}

codefresh/provider.go

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ func Provider() *schema.Provider {
5151
"codefresh_users": dataSourceUsers(),
5252
"codefresh_registry": dataSourceRegistry(),
5353
"codefresh_pipelines": dataSourcePipelines(),
54+
"codefresh_project": dataSourceProject(),
5455
},
5556
ResourcesMap: map[string]*schema.Resource{
5657
"codefresh_account": resourceAccount(),

docs/data-sources/project.md

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
---
2+
page_title: "codefresh_project Data Source - terraform-provider-codefresh"
3+
subcategory: ""
4+
description: |-
5+
This data source retrieves a project by its ID or name.
6+
---
7+
8+
# codefresh_project (Data Source)
9+
10+
This data source retrieves a project by its ID or name.
11+
12+
## Example Usage
13+
14+
```hcl
15+
data "codefresh_project" "myapp" {
16+
name = "myapp"
17+
}
18+
19+
20+
resource "codefresh_pipeline" "myapp-deploy" {
21+
22+
name = "${data.codefresh_project.myapp.projectName}/myapp-deploy"
23+
24+
...
25+
}
26+
27+
```
28+
29+
<!-- schema generated by tfplugindocs -->
30+
## Schema
31+
32+
### Optional
33+
34+
- `_id` (String)
35+
- `name` (String)
36+
- `tags` (List of String)
37+
38+
### Read-Only
39+
40+
- `id` (String) The ID of this resource.
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
page_title: "{{.Name}} {{.Type}} - {{.ProviderName}}"
3+
subcategory: ""
4+
description: |-
5+
{{ .Description | plainmarkdown | trimspace | prefixlines " " }}
6+
---
7+
8+
# {{.Name}} ({{.Type}})
9+
10+
{{ .Description | trimspace }}
11+
12+
## Example Usage
13+
14+
```hcl
15+
data "codefresh_project" "myapp" {
16+
name = "myapp"
17+
}
18+
19+
20+
resource "codefresh_pipeline" "myapp-deploy" {
21+
22+
name = "${data.codefresh_project.myapp.projectName}/myapp-deploy"
23+
24+
...
25+
}
26+
27+
```
28+
29+
{{ .SchemaMarkdown | trimspace }}

0 commit comments

Comments
 (0)