Skip to content

Commit

Permalink
Add datasource plugin_info
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhilsbhat committed Feb 23, 2023
1 parent 1efb0a3 commit 143591c
Show file tree
Hide file tree
Showing 8 changed files with 192 additions and 2 deletions.
38 changes: 38 additions & 0 deletions docs/data-sources/plugin_info.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "gocd_plugin_info Data Source - terraform-provider-gocd"
subcategory: ""
description: |-
---

# gocd_plugin_info (Data Source)
Fetches specific plugin information present in GoCD by interacting with plugin info [api](https://api.gocd.org/current/#plugin-info).

## Example Usage
```terraform
data "gocd_plugin_info" "kubernetes_plugin" {
plugin_id = "cd.go.contrib.secrets.kubernetes"
}
```


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

### Required

- `plugin_id` (String) The unique plugin identifier.

### Optional

- `bundled_plugin` (Boolean) Indicates whether the plugin is bundled with GoCD.
- `etag` (String) Etag used to track the plugin information
- `plugin_file_location` (String) The location where the plugin is installed.
- `status` (Map of String) The status of the plugin.

### Read-Only

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


4 changes: 4 additions & 0 deletions examples/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,8 @@ output "yaml_plugin_settings" {

output "sample_kube_secret_config" {
value = data.gocd_secret_config.sample_kube_secret_config.plugin_id
}

output "kubernetes_plugin" {
value = data.gocd_plugin_info.kubernetes_plugin
}
3 changes: 3 additions & 0 deletions examples/plugin_info.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
data "gocd_plugin_info" "kubernetes_plugin" {
plugin_id = "cd.go.contrib.secrets.kubernetes"
}
4 changes: 2 additions & 2 deletions examples/provider.tf
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ terraform {
provider "gocd" {
base_url = "http://localhost:8153/go"
username = "admin"
// password = "admin"
auth_token = "d8fccbc997d04e917b1490af8e7bf46290ab8c99"
password = "admin"
// auth_token = "d8fccbc997d04e917b1490af8e7bf46290ab8c99"
loglevel = "debug"
// skip_check = true
}
103 changes: 103 additions & 0 deletions internal/provider/data_plugin_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
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 resourcePluginInfo() *schema.Resource {
return &schema.Resource{
ReadContext: datasourcePluginInfoRead,
Schema: map[string]*schema.Schema{
"plugin_id": {
Type: schema.TypeString,
Required: true,
Computed: false,
ForceNew: true,
Description: "The unique plugin identifier.",
},
"plugin_file_location": {
Type: schema.TypeString,
Computed: true,
Optional: true,
Description: "The location where the plugin is installed.",
},
"bundled_plugin": {
Type: schema.TypeBool,
Computed: true,
Optional: true,
Description: "Indicates whether the plugin is bundled with GoCD.",
},
"status": {
Type: schema.TypeSet,
Computed: true,
Optional: true,
Description: "The status of the plugin.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"status": {
Type: schema.TypeString,
Description: "Status of the plugin. Can be one of active, invalid.",
Computed: true,
},
},
},
},
"etag": {
Type: schema.TypeString,
Computed: true,
Optional: true,
Description: "Etag used to track the plugin information",
},
},
}
}

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

id := d.Id()

if len(id) == 0 {
pluginID := utils.String(d.Get(utils.TerraformPluginID))
id = pluginID
}

pluginID := utils.String(d.Get(utils.TerraformPluginID))
response, err := defaultConfig.GetPluginInfo(pluginID)
if err != nil {
return diag.Errorf("getting plugin information of '%s' errored with: %v", pluginID, err)
}

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

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

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

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

d.SetId(id)

return nil
}

func flattenStatus(plugin gocd.Plugin) []map[string]string {
return []map[string]string{
{
"status": plugin.Status.State,
},
}
}
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ func Provider() *schema.Provider {
"gocd_config_repository": dataSourceConfigRepository(),
"gocd_environment": dataSourceEnvironment(),
"gocd_secret_config": dataSourceSecretConfig(),
"gocd_plugin_info": resourcePluginInfo(),
},

ConfigureContextFunc: client.GetGoCDClient,
Expand Down
3 changes: 3 additions & 0 deletions pkg/utils/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ const (
TerraformResourceConfiguration = "configuration"
TerraformResourceClusterProfileID = "cluster_profile_id"
TerraformPluginID = "plugin_id"
TerraformPluginLocation = "plugin_file_location"
TerraformPluginBundled = "bundled_plugin"
TerraformPluginStatus = "status"
TerraformProperties = "properties"
TerraformResourceKey = "key"
TerraformResourceValue = "value"
Expand Down
38 changes: 38 additions & 0 deletions templates/data-sources/plugin_info.md.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "gocd_plugin_info Data Source - terraform-provider-gocd"
subcategory: ""
description: |-

---

# gocd_plugin_info (Data Source)
Fetches specific plugin information present in GoCD by interacting with plugin info [api](https://api.gocd.org/current/#plugin-info).

## Example Usage
```terraform
data "gocd_plugin_info" "kubernetes_plugin" {
plugin_id = "cd.go.contrib.secrets.kubernetes"
}
```


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

### Required

- `plugin_id` (String) The unique plugin identifier.

### Optional

- `bundled_plugin` (Boolean) Indicates whether the plugin is bundled with GoCD.
- `etag` (String) Etag used to track the plugin information
- `plugin_file_location` (String) The location where the plugin is installed.
- `status` (Map of String) The status of the plugin.

### Read-Only

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


0 comments on commit 143591c

Please sign in to comment.