Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Data source project #117

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions codefresh/data_project.go
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
}
1 change: 1 addition & 0 deletions codefresh/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
40 changes: 40 additions & 0 deletions docs/data-sources/project.md
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.
29 changes: 29 additions & 0 deletions templates/data-sources/project.md.tmpl
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 }}
Loading