Skip to content

Commit

Permalink
Add support for datasource gocd_config_repository
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhilsbhat committed Jan 27, 2023
1 parent f2e5f6c commit e921ef4
Show file tree
Hide file tree
Showing 13 changed files with 373 additions and 15 deletions.
8 changes: 3 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,10 @@ local.check: local.fmt ## Loads all the dependencies to vendor directory
go mod vendor
go mod tidy

local.build: local.check ## Generates the artifact with the help of 'go build'
build.local: local.check ## Generates the artifact with the help of 'go build'
go build -o $(APP_NAME)_v$(VERSION) -ldflags="-s -w"

local.push: local.build ## Pushes built artifact to the specified location

local.run: local.build ## Generates the artifact and start the service in the current directory
local.run: build.local ## Generates the artifact and start the service in the current directory
./${APP_NAME}

lint: ## Lint's application for errors, it is a linters aggregator (https://github.com/golangci/golangci-lint).
Expand All @@ -51,7 +49,7 @@ generate.mock: ## generates mocks for the selected source packages.
test: ## runs test cases
@time go test $(TEST_FILES) -mod=vendor -coverprofile cover.out && go tool cover -html=cover.out -o cover.html && open cover.html

create.newversion.tfregistry: local.build ## Sets up the local terraform registry with the version specified.
create.newversion.tfregistry: build.local ## Sets up the local terraform registry with the version specified.
@mkdir -p ~/terraform-providers/registry.terraform.io/hashicorp/gocd/$(VERSION)/darwin_arm64/

upload.newversion.provider: create.newversion.tfregistry ## Uploads the updated provider to local terraform registry.
Expand Down
78 changes: 78 additions & 0 deletions docs/data-sources/config_repository.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "gocd_config_repository Data Source - terraform-provider-gocd"
subcategory: ""
description: |-
---

# gocd_config_repository (Data Source)





<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `profile_id` (String) The identifier of the elastic agent profile.

### Optional

- `configuration` (Block List) the list of configuration properties that represent the configuration of this profile. (see [below for nested schema](#nestedblock--configuration))
- `etag` (String) Etag used to track the plugin settings
- `plugin_id` (String) The plugin identifier of the cluster profile.

### Read-Only

- `id` (String) The ID of this resource.
- `material` (Set of Object) The material to be used by the config repo. (see [below for nested schema](#nestedatt--material))
- `rules` (List of Object) The list of rules, which allows restricting the entities that the config repo can refer to. (see [below for nested schema](#nestedatt--rules))

<a id="nestedblock--configuration"></a>
### Nested Schema for `configuration`

Optional:

- `encrypted_value` (String) The encrypted value of the property
- `is_secure` (Boolean) Specify whether the given property is secure or not. If true and encrypted_value is not specified, GoCD will store the value in encrypted format.
- `key` (String) the name of the property key.
- `value` (String) The value of the property


<a id="nestedatt--material"></a>
### Nested Schema for `material`

Read-Only:

- `attributes` (Set of Object) (see [below for nested schema](#nestedobjatt--material--attributes))
- `fingerprint` (String)
- `type` (String)

<a id="nestedobjatt--material--attributes"></a>
### Nested Schema for `material.attributes`

Read-Only:

- `auto_update` (Boolean)
- `branch` (String)
- `encrypted_password` (String)
- `password` (String)
- `url` (String)
- `username` (String)



<a id="nestedatt--rules"></a>
### Nested Schema for `rules`

Read-Only:

- `action` (String)
- `directive` (String)
- `resource` (String)
- `type` (String)


10 changes: 8 additions & 2 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@ description: |-
---

# gocd Provider

# GoCD Provider

## Example Usage

A provider for a continuous delivery server [GoCD](https://www.gocd.org/).

```hcl
provider "gocd" {
# configuration for gocd goes here
}
```

<!-- schema generated by tfplugindocs -->
## Schema
Expand Down
4 changes: 4 additions & 0 deletions examples/config_repo.tf
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,8 @@ resource "gocd_config_repository" "sample_config_repo" {
"resource" : "*"
}
]
}

data "gocd_config_repository" "sample_config_repo" {
profile_id = "sample_config_repo"
}
1 change: 0 additions & 1 deletion examples/environment.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ resource "gocd_environment" "sample_environment" {
pipelines = [
"gocd-prometheus-exporter",
"helm-images",
"action-movies-manual",
]
environment_variables {
name = "TEST_ENV11"
Expand Down
4 changes: 4 additions & 0 deletions examples/outputs.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
output "encrypted_value" {
value = gocd_encrypt_value.new_value.encrypted_value
sensitive = true
}

output "sample_config_repo" {
value = data.gocd_config_repository.sample_config_repo.material
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ require (
github.com/huandu/xstrings v1.3.2 // indirect
github.com/imdario/mergo v0.3.13 // indirect
github.com/jinzhu/copier v0.3.5 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/mitchellh/cli v1.1.4 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
Expand Down
8 changes: 6 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,15 @@ github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0
github.com/matryer/is v1.2.0/go.mod h1:2fLPjFQM9rhQ15aVEtbuwhJinnOqrmgXPNdZsdwlWXA=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40=
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mitchellh/cli v1.1.4 h1:qj8czE26AU4PbiaPXK5uVmMSM+V5BYsFBiM9HhGRLUA=
github.com/mitchellh/cli v1.1.4/go.mod h1:vTLESy5mRhKOs9KDp0/RATawxP1UqBmdrpVRMnpcvKQ=
github.com/mitchellh/copystructure v1.0.0/go.mod h1:SNtv71yrdKgLRyLFxmLdkAbkKEFWgYaq1OVrnRcwhnw=
Expand Down Expand Up @@ -319,6 +322,7 @@ golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220503163025-988cb79eb6c6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.4.0 h1:Zr2JFtRQNX3BCZ8YtxRE9hNJYC8J6I1MVbMg6owUp18=
golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
Expand Down
93 changes: 93 additions & 0 deletions internal/provider/datasource_config_repository.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package provider

import (
"context"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/nikhilsbhat/gocd-sdk-go"
"github.com/nikhilsbhat/terraform-provider-gocd/pkg/utils"
)

func dataSourceConfigRepository() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceConfigRepositoryRead,
Schema: configRepoSchema(),
}
}

func dataSourceConfigRepositoryRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
defaultConfig := meta.(gocd.GoCd)

id := d.Id()

if len(id) == 0 {
newID, err := utils.GetRandomID()
if err != nil {
d.SetId("")

return diag.Errorf("errored while fetching randomID %v", err)
}
id = newID
}

profileID := utils.String(d.Get(utils.TerraformResourceProfileID))
response, err := defaultConfig.GetConfigRepo(profileID)
if err != nil {
return diag.Errorf("getting config repo %s errored with: %v", profileID, err)
}

if err = d.Set(utils.TerraformPluginID, response.PluginID); err != nil {
return diag.Errorf("setting pluginID errored with %v", err)
}

if err = d.Set(utils.TerraformResourceMaterial, flattenMaterial(response.Material)); err != nil {
return diag.Errorf("setting material errored with %v", err)
}

flattenedConfiguration, err := utils.MapSlice(response.Configuration)
if err != nil {
d.SetId("")

return diag.Errorf("errored while flattening Configuration obtained: %v", err)
}

if err = d.Set(utils.TerraformResourceConfiguration, flattenedConfiguration); err != nil {
return diag.Errorf("setting plugin configurations errored with %v", err)
}

if err = d.Set(utils.TerraformResourceRules, response.Rules); err != nil {
return diag.Errorf("setting rules errored with %v", err)
}

if err = d.Set(utils.TerraformResourceEtag, response.ETAG); err != nil {
return diag.Errorf("setting etag errored with %v", err)
}

d.SetId(id)

return nil
}

func flattenMaterial(material gocd.Material) []map[string]interface{} {
return []map[string]interface{}{
{
"type": material.Type,
"fingerprint": material.Fingerprint,
"attributes": flattenAttributes(material.Attributes),
},
}
}

func flattenAttributes(attribute gocd.Attribute) []map[string]interface{} {
return []map[string]interface{}{
{
"url": attribute.URL,
"username": attribute.Username,
"password": attribute.Password,
"encrypted_password": attribute.EncryptedPassword,
"branch": attribute.Branch,
"auto_update": attribute.AutoUpdate,
},
}
}
4 changes: 2 additions & 2 deletions internal/provider/environments.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func getEnvChanges(d *schema.ResourceData) (environmentChanges, error) {

envVars, err := getEnvironments(oldVars)
if err != nil {
return changes, fmt.Errorf("reading environment errored with %v", err)
return changes, fmt.Errorf("reading environment errored with %w", err)
}

changes.equal = true
Expand All @@ -194,7 +194,7 @@ func getEnvChanges(d *schema.ResourceData) (environmentChanges, error) {
if !cmp.Equal(oldVars, newVars) {
envVars, err = getEnvironments(newVars)
if err != nil {
return changes, fmt.Errorf("reading environment errored with %v", err)
return changes, fmt.Errorf("reading environment errored with %w", err)
}
changes.envVarsChanges = envVars
changes.equal = false
Expand Down
4 changes: 3 additions & 1 deletion internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ func Provider() *schema.Provider {
"gocd_environment": resourceEnvironment(),
},

DataSourcesMap: map[string]*schema.Resource{},
DataSourcesMap: map[string]*schema.Resource{
"gocd_config_repository": dataSourceConfigRepository(),
},

ConfigureContextFunc: client.GetGoCDClient,
}
Expand Down
Loading

0 comments on commit e921ef4

Please sign in to comment.