From 1a66e3beea03a5d0d53a88d2583c10a09af5fd14 Mon Sep 17 00:00:00 2001 From: Jake Nelson Date: Mon, 8 May 2023 08:12:51 +0000 Subject: [PATCH 1/5] Add project data source --- codefresh/data_project.go | 69 ++++++++++++++++++++++++++ codefresh/provider.go | 1 + docs/data-sources/project.md | 40 +++++++++++++++ templates/data-sources/project.md.tmpl | 29 +++++++++++ 4 files changed, 139 insertions(+) create mode 100644 codefresh/data_project.go create mode 100644 docs/data-sources/project.md create mode 100644 templates/data-sources/project.md.tmpl diff --git a/codefresh/data_project.go b/codefresh/data_project.go new file mode 100644 index 0000000..3815121 --- /dev/null +++ b/codefresh/data_project.go @@ -0,0 +1,69 @@ +package codefresh + +import ( + "fmt" + + cfClient "github.com/codefresh-io/terraform-provider-codefresh/client" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +func dataSourceProject() *schema.Resource { + return &schema.Resource{ + Description: "This data source retrieves a team 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_team - cannot find team") + } + + 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 +} diff --git a/codefresh/provider.go b/codefresh/provider.go index fba2329..9cf0793 100644 --- a/codefresh/provider.go +++ b/codefresh/provider.go @@ -40,6 +40,7 @@ func Provider() *schema.Provider { "codefresh_users": dataSourceUsers(), "codefresh_registry": dataSourceRegistry(), "codefresh_pipelines": dataSourcePipelines(), + "codefresh_project": dataSourceProject(), }, ResourcesMap: map[string]*schema.Resource{ "codefresh_account": resourceAccount(), diff --git a/docs/data-sources/project.md b/docs/data-sources/project.md new file mode 100644 index 0000000..82a5cc4 --- /dev/null +++ b/docs/data-sources/project.md @@ -0,0 +1,40 @@ +--- +page_title: "codefresh_project Data Source - terraform-provider-codefresh" +subcategory: "" +description: |- + This data source retrieves a team by its ID or name. +--- + +# codefresh_project (Data Source) + +This data source retrieves a team 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 + +### Optional + +- `_id` (String) +- `name` (String) +- `tags` (List of String) + +### Read-Only + +- `id` (String) The ID of this resource. \ No newline at end of file diff --git a/templates/data-sources/project.md.tmpl b/templates/data-sources/project.md.tmpl new file mode 100644 index 0000000..3b0f8fe --- /dev/null +++ b/templates/data-sources/project.md.tmpl @@ -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 }} \ No newline at end of file From ea655252ceebcee14906b6991961229a8ca10767 Mon Sep 17 00:00:00 2001 From: Jake Nelson Date: Mon, 8 May 2023 08:18:36 +0000 Subject: [PATCH 2/5] Correct comment --- codefresh/data_project.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/codefresh/data_project.go b/codefresh/data_project.go index 3815121..1885638 100644 --- a/codefresh/data_project.go +++ b/codefresh/data_project.go @@ -9,7 +9,7 @@ import ( func dataSourceProject() *schema.Resource { return &schema.Resource{ - Description: "This data source retrieves a team by its ID or name.", + Description: "This data source retrieves a project by its ID or name.", Read: dataSourceProjectRead, Schema: map[string]*schema.Schema{ "_id": { From 14a60b282845286122338d73c9e680235c2c343b Mon Sep 17 00:00:00 2001 From: Jake Nelson Date: Tue, 9 May 2023 16:35:30 +1000 Subject: [PATCH 3/5] Fix typo --- docs/data-sources/project.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/data-sources/project.md b/docs/data-sources/project.md index 82a5cc4..8569264 100644 --- a/docs/data-sources/project.md +++ b/docs/data-sources/project.md @@ -2,12 +2,12 @@ page_title: "codefresh_project Data Source - terraform-provider-codefresh" subcategory: "" description: |- - This data source retrieves a team by its ID or name. + This data source retrieves a project by its ID or name. --- # codefresh_project (Data Source) -This data source retrieves a team by its ID or name. +This data source retrieves a project by its ID or name. ## Example Usage @@ -37,4 +37,4 @@ resource "codefresh_pipeline" "myapp-deploy" { ### Read-Only -- `id` (String) The ID of this resource. \ No newline at end of file +- `id` (String) The ID of this resource. From d5d2f729208bc90c040c2bda7a27d7885e4a08d0 Mon Sep 17 00:00:00 2001 From: Jake Nelson Date: Tue, 9 May 2023 16:36:24 +1000 Subject: [PATCH 4/5] Fix reference to team --- codefresh/data_project.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/codefresh/data_project.go b/codefresh/data_project.go index 1885638..43b73c9 100644 --- a/codefresh/data_project.go +++ b/codefresh/data_project.go @@ -48,7 +48,7 @@ func dataSourceProjectRead(d *schema.ResourceData, meta interface{}) error { } if project == nil { - return fmt.Errorf("data.codefresh_team - cannot find team") + return fmt.Errorf("data.codefresh_project - cannot find project") } return mapDataProjectToResource(project, d) From 8eae30f8353ca7916990439fe8dc8908160a064f Mon Sep 17 00:00:00 2001 From: Ilia Medvedev Date: Sun, 25 Feb 2024 10:54:32 +0200 Subject: [PATCH 5/5] merge upstream and update docs --- codefresh/data_project.go | 2 +- docs/data-sources/project.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/codefresh/data_project.go b/codefresh/data_project.go index 43b73c9..e0f464b 100644 --- a/codefresh/data_project.go +++ b/codefresh/data_project.go @@ -3,7 +3,7 @@ package codefresh import ( "fmt" - cfClient "github.com/codefresh-io/terraform-provider-codefresh/client" + cfClient "github.com/codefresh-io/terraform-provider-codefresh/codefresh/cfclient" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" ) diff --git a/docs/data-sources/project.md b/docs/data-sources/project.md index 8569264..d19d7f6 100644 --- a/docs/data-sources/project.md +++ b/docs/data-sources/project.md @@ -37,4 +37,4 @@ resource "codefresh_pipeline" "myapp-deploy" { ### Read-Only -- `id` (String) The ID of this resource. +- `id` (String) The ID of this resource. \ No newline at end of file