Skip to content

Commit

Permalink
Feat: Data source project (#117)
Browse files Browse the repository at this point in the history
## 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
3 people authored Feb 25, 2024
1 parent 87d6913 commit 353f7d9
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 0 deletions.
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 }}

0 comments on commit 353f7d9

Please sign in to comment.