-
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.
## 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]>
- Loading branch information
1 parent
87d6913
commit 353f7d9
Showing
4 changed files
with
139 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package codefresh | ||
|
||
import ( | ||
"fmt" | ||
|
||
cfClient "github.com/codefresh-io/terraform-provider-codefresh/codefresh/cfclient" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func dataSourceProject() *schema.Resource { | ||
return &schema.Resource{ | ||
Description: "This data source retrieves a project by its ID or name.", | ||
Read: dataSourceProjectRead, | ||
Schema: map[string]*schema.Schema{ | ||
"_id": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
"name": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
"tags": { | ||
Type: schema.TypeList, | ||
Optional: true, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceProjectRead(d *schema.ResourceData, meta interface{}) error { | ||
|
||
client := meta.(*cfClient.Client) | ||
var project *cfClient.Project | ||
var err error | ||
|
||
if _id, _idOk := d.GetOk("_id"); _idOk { | ||
project, err = client.GetProjectByID(_id.(string)) | ||
} else if name, nameOk := d.GetOk("name"); nameOk { | ||
project, err = client.GetProjectByName(name.(string)) | ||
} | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
if project == nil { | ||
return fmt.Errorf("data.codefresh_project - cannot find project") | ||
} | ||
|
||
return mapDataProjectToResource(project, d) | ||
|
||
} | ||
|
||
func mapDataProjectToResource(project *cfClient.Project, d *schema.ResourceData) error { | ||
|
||
if project == nil || project.ID == "" { | ||
return fmt.Errorf("data.codefresh_project - failed to mapDataProjectToResource") | ||
} | ||
d.SetId(project.ID) | ||
|
||
d.Set("_id", project.ID) | ||
d.Set("tags", project.Tags) | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
--- | ||
page_title: "codefresh_project Data Source - terraform-provider-codefresh" | ||
subcategory: "" | ||
description: |- | ||
This data source retrieves a project by its ID or name. | ||
--- | ||
|
||
# codefresh_project (Data Source) | ||
|
||
This data source retrieves a project by its ID or name. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
data "codefresh_project" "myapp" { | ||
name = "myapp" | ||
} | ||
resource "codefresh_pipeline" "myapp-deploy" { | ||
name = "${data.codefresh_project.myapp.projectName}/myapp-deploy" | ||
... | ||
} | ||
``` | ||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Optional | ||
|
||
- `_id` (String) | ||
- `name` (String) | ||
- `tags` (List of String) | ||
|
||
### Read-Only | ||
|
||
- `id` (String) The ID of this resource. |
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,29 @@ | ||
--- | ||
page_title: "{{.Name}} {{.Type}} - {{.ProviderName}}" | ||
subcategory: "" | ||
description: |- | ||
{{ .Description | plainmarkdown | trimspace | prefixlines " " }} | ||
--- | ||
|
||
# {{.Name}} ({{.Type}}) | ||
|
||
{{ .Description | trimspace }} | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
data "codefresh_project" "myapp" { | ||
name = "myapp" | ||
} | ||
|
||
|
||
resource "codefresh_pipeline" "myapp-deploy" { | ||
|
||
name = "${data.codefresh_project.myapp.projectName}/myapp-deploy" | ||
|
||
... | ||
} | ||
|
||
``` | ||
|
||
{{ .SchemaMarkdown | trimspace }} |