diff --git a/codefresh/data_project.go b/codefresh/data_project.go new file mode 100644 index 0000000..e0f464b --- /dev/null +++ b/codefresh/data_project.go @@ -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 +} diff --git a/codefresh/provider.go b/codefresh/provider.go index c79e808..0a41589 100644 --- a/codefresh/provider.go +++ b/codefresh/provider.go @@ -51,6 +51,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..d19d7f6 --- /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 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 + +### 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