Skip to content

Commit

Permalink
Add support for resource gocd_agent
Browse files Browse the repository at this point in the history
Rename the datasource gocd_agent_config to gocd_agent to keep it in sync with resource
  • Loading branch information
nikhilsbhat committed Mar 13, 2023
1 parent 10f8000 commit 4d67fd1
Show file tree
Hide file tree
Showing 14 changed files with 241 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "gocd_agent_config Data Source - terraform-provider-gocd"
page_title: "gocd_agent Data Source - terraform-provider-gocd"
subcategory: ""
description: |-
---

# gocd_agent_config (Data Source)
# gocd_agent (Data Source)
Fetch the details of the GoCD Agent by interacting with GET agent [api](https://api.gocd.org/current/#get-one-agent).

## Example Usage
```terraform
data "gocd_agent_config" "sample_agent" {
data "gocd_agent" "sample_agent" {
uuid = "4c92e7e3-8abd-4b02-a7eb-c46b2c7ac674"
}
```
Expand Down
44 changes: 44 additions & 0 deletions docs/resources/agent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "gocd_agent Resource - terraform-provider-gocd"
subcategory: ""
description: |-
---

# gocd_agent (Resource)
Update configuration of a specified agent by interacting with GoCD [api](https://api.gocd.org/current/#update-an-agent).

## Example Usage
```terraform
resource "gocd_agent" "sample_agent" {
uuid = "bbfe3a75-7fd8-48db-af32-0a91b9efd0ab"
agent_config_state = "ENABLED"
environments = ["sample_environment_3"]
resources = ["linux"]
hostname = "sample.agent001.com"
}
```
**NOTE:** Since this resource updates the agent config, `terraform destroy` of this would just remove its reference from state and does not delete the agent itself.

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

### Required

- `uuid` (String) The identifier of this agent.

### Optional

- `agent_config_state` (String) Whether an agent is enabled or not. Can be one of `Enabled`, `Disabled`.
- `environments` (List of String) The set of environments that this agent belongs to.
- `hostname` (String) The hostname of the agent.
- `ip_address` (String) The IP address of the agent.
- `operating_system` (String) The operating system as reported by the agent.
- `resources` (List of String) The set of resources that this agent is tagged with (if agent is not an elastic agent).

### Read-Only

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


9 changes: 7 additions & 2 deletions examples/agents_config.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
data "gocd_agent_config" "sample_agent" {
uuid = "3bc47e62-eb8f-4c37-b082-1517f10ef7aa"
resource "gocd_agent" "sample_agent" {
uuid = "bbfe3a75-7fd8-48db-af32-0a91b9efd0ab"
environments = ["sample_environment_3"]
}

data "gocd_agent" "sample_agent" {
uuid = "bbfe3a75-7fd8-48db-af32-0a91b9efd0ab"
}
4 changes: 0 additions & 4 deletions examples/backup_config.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,3 @@ resource "gocd_backup_config" "nightly_backup" {
schedule = "0 0 2 * * ?"
post_backup_script = "path/to/postbackup_script.sh"
}

resource "gocd_backup_schedule" "now" {
schedule = true
}
3 changes: 3 additions & 0 deletions examples/backup_schedue.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
//resource "gocd_backup_schedule" "now" {
// schedule = true
//}
2 changes: 1 addition & 1 deletion examples/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ output "kubernetes_plugin" {
}

output "sample_agent_config" {
value = data.gocd_agent_config.sample_agent.hostname
value = data.gocd_agent.sample_agent.hostname
}
2 changes: 1 addition & 1 deletion examples/provider.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ terraform {
required_providers {
gocd = {
source = "hashicorp/gocd"
version = "~> 0.0.1"
version = "~> 0.0.3"
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/hashicorp/terraform-plugin-docs v0.13.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.24.1
github.com/mitchellh/mapstructure v1.5.0
github.com/nikhilsbhat/gocd-sdk-go v0.1.1-0.20230309041138-a419ad58c924
github.com/nikhilsbhat/gocd-sdk-go v0.1.1-0.20230313044033-c9dfdc7560a0
github.com/spf13/cast v1.5.0
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,8 @@ github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx
github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zxSIeXaQ=
github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/nikhilsbhat/gocd-sdk-go v0.1.1-0.20230309041138-a419ad58c924 h1:WEiTpHnBVViVXjS4BEmj6lNF5ah1A1ts7lCmFUAqOFo=
github.com/nikhilsbhat/gocd-sdk-go v0.1.1-0.20230309041138-a419ad58c924/go.mod h1:3XwSMe/nFH/I0Kt2+ToKKWFyD6yvJb4HaoP0dBHytY4=
github.com/nikhilsbhat/gocd-sdk-go v0.1.1-0.20230313044033-c9dfdc7560a0 h1:QR1QyJwTpNbYZxUHDkuP4SUFPWNxJF1/xnVYLAegLJA=
github.com/nikhilsbhat/gocd-sdk-go v0.1.1-0.20230313044033-c9dfdc7560a0/go.mod h1:3XwSMe/nFH/I0Kt2+ToKKWFyD6yvJb4HaoP0dBHytY4=
github.com/nsf/jsondiff v0.0.0-20200515183724-f29ed568f4ce h1:RPclfga2SEJmgMmz2k+Mg7cowZ8yv4Trqw9UsJby758=
github.com/oklog/run v1.0.0 h1:Ru7dDtJNOyC66gQ5dQmaCa0qIsAUFY3sFpK1Xk8igrw=
github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA=
Expand Down
6 changes: 4 additions & 2 deletions internal/provider/data_agent_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,13 @@ func datasourceAgentRead(ctx context.Context, d *schema.ResourceData, meta inter
return nil
}

func flattenEnvironments(environments []gocd.Environment) []string {
func flattenEnvironments(envs any) []string {
envList := make([]string, 0)
environments := envs.([]interface{})

for _, environment := range environments {
envList = append(envList, environment.Name)
newEnvironment := environment.(map[string]interface{})
envList = append(envList, newEnvironment["name"].(string))
}

return envList
Expand Down
4 changes: 2 additions & 2 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func Provider() *schema.Provider {
"gocd_secret_config": resourceSecretConfig(),
"gocd_backup_config": resourceBackupConfig(),
"gocd_backup_schedule": resourceBackupSchedule(),
// "gocd_agent": resourceAgentConfig(),
"gocd_agent": resourceAgentConfig(),
},

DataSourcesMap: map[string]*schema.Resource{
Expand All @@ -109,7 +109,7 @@ func Provider() *schema.Provider {
"gocd_environment": dataSourceEnvironment(),
"gocd_secret_config": dataSourceSecretConfig(),
"gocd_plugin_info": dataSourcePluginInfo(),
"gocd_agent_config": dataSourceAgentConfig(),
"gocd_agent": dataSourceAgentConfig(),
},

ConfigureContextFunc: client.GetGoCDClient,
Expand Down
128 changes: 126 additions & 2 deletions internal/provider/resource_agent_config.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,132 @@
package provider

import "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
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"
)

//nolint:deadcode,unused
func resourceAgentConfig() *schema.Resource {
return &schema.Resource{
CreateContext: resourceAgentConfigCreate,
ReadContext: resourceAgentConfigRead,
DeleteContext: resourceAgentConfigDelete,
Schema: map[string]*schema.Schema{
"uuid": {
Type: schema.TypeString,
Required: true,
Computed: false,
ForceNew: true,
Description: "The identifier of this agent.",
},
"hostname": {
Type: schema.TypeString,
Optional: true,
Computed: false,
ForceNew: true,
Description: "The hostname of the agent.",
},
"agent_config_state": {
Type: schema.TypeString,
Optional: true,
Computed: false,
ForceNew: true,
Description: "Whether an agent is enabled or not. Can be one of `Enabled`, `Disabled`.",
},
"resources": {
Type: schema.TypeList,
Optional: true,
Computed: false,
ForceNew: true,
Description: "The set of resources that this agent is tagged with (if agent is not an elastic agent).",
Elem: &schema.Schema{Type: schema.TypeString},
},
"environments": {
Type: schema.TypeList,
Optional: true,
Computed: false,
ForceNew: true,
Description: "The set of environments that this agent belongs to.",
Elem: &schema.Schema{Type: schema.TypeString},
},
"ip_address": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: false,
Description: "The IP address of the agent.",
},
"operating_system": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: false,
Description: "The operating system as reported by the agent.",
},
},
}
}

func resourceAgentConfigCreate(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.TerraformResourceUUID))
id = resourceID
}

cfg := gocd.Agent{
ID: id,
Name: utils.String(d.Get(utils.TerraformResourceHostname)),
Environments: utils.GetSlice(d.Get(utils.TerraformResourceEnvironments).([]interface{})),
Resources: utils.GetSlice(d.Get(utils.TerraformResourceResources).([]interface{})),
ConfigState: utils.String(d.Get(utils.TerraformResourceAgentConfigState)),
}

if err := defaultConfig.UpdateAgent(cfg); err != nil {
return diag.Errorf("updating agent '%s' errored with %v", id, err)
}

d.SetId(id)

return resourceAgentConfigRead(ctx, d, meta)
}

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

response, err := defaultConfig.GetAgent(d.Id())
if err != nil {
return diag.Errorf("fetching information of agent '%s' errored with %v", d.Id(), err)
}

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

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

return nil
}

func resourceAgentConfigDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
id := d.Id()
if len(d.Id()) == 0 {
return diag.Errorf("resource with the ID '%s' not found", id)
}

d.SetId("")

return nil
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "gocd_agent_config Data Source - terraform-provider-gocd"
page_title: "gocd_agent Data Source - terraform-provider-gocd"
subcategory: ""
description: |-

---

# gocd_agent_config (Data Source)
# gocd_agent (Data Source)
Fetch the details of the GoCD Agent by interacting with GET agent [api](https://api.gocd.org/current/#get-one-agent).

## Example Usage
```terraform
data "gocd_agent_config" "sample_agent" {
data "gocd_agent" "sample_agent" {
uuid = "4c92e7e3-8abd-4b02-a7eb-c46b2c7ac674"
}
```
Expand Down
44 changes: 44 additions & 0 deletions templates/resources/agent.md.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "gocd_agent Resource - terraform-provider-gocd"
subcategory: ""
description: |-

---

# gocd_agent (Resource)
Update configuration of a specified agent by interacting with GoCD [api](https://api.gocd.org/current/#update-an-agent).

## Example Usage
```terraform
resource "gocd_agent" "sample_agent" {
uuid = "bbfe3a75-7fd8-48db-af32-0a91b9efd0ab"
agent_config_state = "ENABLED"
environments = ["sample_environment_3"]
resources = ["linux"]
hostname = "sample.agent001.com"
}
```
**NOTE:** Since this resource updates the agent config, `terraform destroy` of this would just remove its reference from state and does not delete the agent itself.

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

### Required

- `uuid` (String) The identifier of this agent.

### Optional

- `agent_config_state` (String) Whether an agent is enabled or not. Can be one of `Enabled`, `Disabled`.
- `environments` (List of String) The set of environments that this agent belongs to.
- `hostname` (String) The hostname of the agent.
- `ip_address` (String) The IP address of the agent.
- `operating_system` (String) The operating system as reported by the agent.
- `resources` (List of String) The set of resources that this agent is tagged with (if agent is not an elastic agent).

### Read-Only

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


0 comments on commit 4d67fd1

Please sign in to comment.