Skip to content

Commit

Permalink
Add support for resource and datasource gocd_artifact_store
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhilsbhat committed Apr 27, 2023
1 parent b5bc088 commit 1d72037
Show file tree
Hide file tree
Showing 11 changed files with 453 additions and 1 deletion.
50 changes: 50 additions & 0 deletions docs/data-sources/artifact_store.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "gocd_artifact_store Data Source - terraform-provider-gocd"
subcategory: ""
description: |-
---

# gocd_artifact_store (Data Source)
Fetch the details of the specified artifact store by interacting with GET artifact store [api](https://api.gocd.org/current/#get-an-artifact-store).

## Example Usage
```terraform
data "gocd_artifact_store" "docker" {
store_id = gocd_artifact_store.docker.id
}
```


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

### Required

- `store_id` (String) The identifier of the artifact store.

### Optional

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

### Read-Only

- `id` (String) The ID of this resource.

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

Required:

- `key` (String) the name of the property key.

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.
- `value` (String) The value of the property


56 changes: 56 additions & 0 deletions docs/resources/artifact_store.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "gocd_artifact_store Resource - terraform-provider-gocd"
subcategory: ""
description: |-
---

# gocd_artifact_store (Resource)
Creates artifact store in GoCD with all below passed parameters by interacting with GoCD artifact store [api](https://api.gocd.org/current/#create-an-artifact-store).

## Example Usage
```terraform
resource "gocd_artifact_store" "docker" {
store_id = "docker"
plugin_id = "cd.go.artifact.s3"
properties {
key = "S3Bucket"
value = "sample"
}
properties {
key = "Region"
value = "ap-south-1"
}
}
```


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

### Required

- `plugin_id` (String) The plugin identifier of the artifact plugin.
- `properties` (Block Set, Min: 1) The list of configuration properties that represent the configuration of the profile. (see [below for nested schema](#nestedblock--properties))
- `store_id` (String) The identifier of the artifact store.

### Read-Only

- `etag` (String) etag used to track the plugin settings
- `id` (String) The ID of this resource.

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

Required:

- `key` (String) the name of the property key.

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.
- `value` (String) The value of the property


16 changes: 16 additions & 0 deletions examples/artifact_store.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
resource "gocd_artifact_store" "docker" {
store_id = "docker"
plugin_id = "cd.go.artifact.s3"
properties {
key = "S3Bucket"
value = "sample"
}
properties {
key = "Region"
value = "ap-south-1"
}
}

data "gocd_artifact_store" "docker" {
store_id = gocd_artifact_store.docker.id
}
4 changes: 4 additions & 0 deletions examples/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,7 @@ output "helm_images" {
output "helm_drift" {
value = gocd_pipeline.helm_drift.config
}

output "docker_artifact_store" {
value = data.gocd_artifact_store.docker.properties
}
83 changes: 83 additions & 0 deletions internal/provider/data_artifact_store.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
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 dataSourceArtifactStore() *schema.Resource {
return &schema.Resource{
ReadContext: datasourceArtifactStoreRead,
Schema: map[string]*schema.Schema{
"store_id": {
Type: schema.TypeString,
Required: true,
Computed: false,
ForceNew: true,
Description: "The identifier of the artifact store.",
},
"plugin_id": {
Type: schema.TypeString,
Computed: true,
Optional: true,
Description: "The plugin identifier of the artifact plugin.",
},
"properties": {
Type: schema.TypeList,
Computed: true,
Optional: true,
Description: "The list of configuration properties that represent the configuration of this artifact store.",
Elem: propertiesSchemaData(),
},
"etag": {
Type: schema.TypeString,
Computed: true,
Optional: true,
Description: "Etag used to track an artifact store",
},
},
}
}

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

id := d.Id()

if len(id) == 0 {
resourceID := utils.String(d.Get(utils.TerraformResourceStoreID))
id = resourceID
}

response, err := defaultConfig.GetArtifactStore(id)
if err != nil {
return diag.Errorf("getting artifact store '%s' errored with: %v", id, err)
}

if err = d.Set(utils.TerraformResourcePluginID, response.PluginID); err != nil {
return diag.Errorf(settingAttrErrorTmp, err, utils.TerraformResourcePluginID)
}

if err = d.Set(utils.TerraformResourceEtag, response.ETAG); err != nil {
return diag.Errorf(settingAttrErrorTmp, utils.TerraformResourceEtag, err)
}

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

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

if err = d.Set(utils.TerraformResourceProperties, flattenedProperties); err != nil {
return diag.Errorf(settingAttrErrorTmp, err, utils.TerraformResourceProperties)
}

d.SetId(id)

return nil
}
2 changes: 1 addition & 1 deletion internal/provider/gocd_provider_schemas.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func propertiesSchemaResource() *schema.Schema {
Type: schema.TypeSet,
Required: true,
Computed: false,
Description: "the list of configuration properties that represent the configuration of this profile.",
Description: "The list of configuration properties that represent the configuration of the profile.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"key": {
Expand Down
2 changes: 2 additions & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ func Provider() *schema.Provider {
"gocd_backup_schedule": resourceBackupSchedule(),
"gocd_agent": resourceAgentConfig(),
"gocd_pipeline": resourcePipeline(),
"gocd_artifact_store": resourceArtifactStore(),
},

DataSourcesMap: map[string]*schema.Resource{
Expand All @@ -112,6 +113,7 @@ func Provider() *schema.Provider {
"gocd_plugin_info": dataSourcePluginInfo(),
"gocd_agent": dataSourceAgentConfig(),
"gocd_pipeline": dataSourcePipeline(),
"gocd_artifact_store": dataSourceArtifactStore(),
},

ConfigureContextFunc: client.GetGoCDClient,
Expand Down
134 changes: 134 additions & 0 deletions internal/provider/resource_artifact_store.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package provider

import (
"context"
"log"

"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 resourceArtifactStore() *schema.Resource {
return &schema.Resource{
CreateContext: resourceArtifactStoreCreate,
ReadContext: resourceArtifactStoreRead,
DeleteContext: resourceArtifactStoreDelete,
UpdateContext: resourceArtifactStoreUpdate,
Schema: map[string]*schema.Schema{
"store_id": {
Type: schema.TypeString,
Required: true,
Computed: false,
ForceNew: true,
Description: "The identifier of the artifact store.",
},
"plugin_id": {
Type: schema.TypeString,
Required: true,
Computed: false,
ForceNew: true,
Description: "The plugin identifier of the artifact plugin.",
},
"properties": propertiesSchemaResource(),
"etag": {
Type: schema.TypeString,
Required: false,
Computed: true,
ForceNew: false,
Description: "etag used to track the plugin settings",
},
},
}
}

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

if !d.IsNewResource() {
return nil
}

id := d.Id()

if len(id) == 0 {
resourceID := utils.String(d.Get(utils.TerraformResourceStoreID))
id = resourceID
}

cfg := gocd.CommonConfig{
ID: id,
PluginID: utils.String(d.Get(utils.TerraformResourcePluginID)),
Properties: getPluginConfiguration(d.Get(utils.TerraformResourceProperties)),
}

_, err := defaultConfig.CreateArtifactStore(cfg)
if err != nil {
return diag.Errorf("creating artifact store '%s' for plugin '%s' errored with %v", cfg.ID, cfg.PluginID, err)
}

d.SetId(id)

return resourceArtifactStoreRead(ctx, d, meta)
}

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

storeID := utils.String(d.Get(utils.TerraformResourceStoreID))
response, err := defaultConfig.GetArtifactStore(storeID)
if err != nil {
return diag.Errorf("getting artifact store configuration '%s' errored with: %v", storeID, err)
}

if err = d.Set(utils.TerraformResourceEtag, response.ETAG); err != nil {
return diag.Errorf(settingAttrErrorTmp, utils.TerraformResourceEtag, err)
}

return nil
}

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

if !d.HasChange(utils.TerraformResourceProperties) {
log.Printf("nothing to update so skipping")

return nil
}

cfg := gocd.CommonConfig{
ID: utils.String(d.Get(utils.TerraformResourceStoreID)),
PluginID: utils.String(d.Get(utils.TerraformResourcePluginID)),
Properties: getPluginConfiguration(d.Get(utils.TerraformResourceProperties)),
ETAG: utils.String(d.Get(utils.TerraformResourceEtag)),
}

_, err := defaultConfig.UpdateArtifactStore(cfg)
if err != nil {
return diag.Errorf("updating artifact store config '%s' errored with: %v", cfg.ID, err)
}

return resourceArtifactStoreRead(ctx, d, meta)
}

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

id := d.Id()
if len(d.Id()) == 0 {
return diag.Errorf("resource with the ID '%s' not found", id)
}

storeID := utils.String(d.Get(utils.TerraformResourceStoreID))

err := defaultConfig.DeleteArtifactStore(storeID)
if err != nil {
return diag.Errorf("deleting artifact store '%s' errored with: %v", storeID, err)
}

d.SetId("")

return nil
}
1 change: 1 addition & 0 deletions pkg/utils/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package utils

const (
TerraformResourceProfileID = "profile_id"
TerraformResourceStoreID = "store_id"
TerraformResourceConfigRepo = "config_repos"
TerraformResourcePluginConfiguration = "plugin_configurations"
TerraformResourceConfiguration = "configuration"
Expand Down
Loading

0 comments on commit 1d72037

Please sign in to comment.