Skip to content

Commit

Permalink
Add datasource auth_config, environment, cluster_profile and elastic_…
Browse files Browse the repository at this point in the history
…agent_profile
  • Loading branch information
nikhilsbhat committed Jan 28, 2023
1 parent e921ef4 commit 316d1dc
Show file tree
Hide file tree
Showing 18 changed files with 495 additions and 12 deletions.
4 changes: 4 additions & 0 deletions examples/authorization_configuration.tf
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@ resource "gocd_auth_config" "password_file_config" {
key = "PasswordFilePath"
value = "/Users/nikhil.bhat/idfc/gocd-setup/.gocdadmin2"
}
}

data "gocd_auth_config" "password_file_config" {
profile_id = "admin_new"
}
4 changes: 4 additions & 0 deletions examples/cluster_profile.tf
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,8 @@ resource "gocd_cluster_profile" "ec2_cluster_profile" {
key = "aws_secret_access_key"
value = "dsdfmldfkjgcdfjgcdjfdfjgkcdfgfn"
}
}

data "gocd_cluster_profile" "ec2_cluster_profile" {
profile_id = "ec2_cluster_profile"
}
4 changes: 4 additions & 0 deletions examples/elastic_agent_profile.tf
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,8 @@ resource "gocd_elastic_agent_profile" "sample_ec2" {
key = "ec2_user_data"
value = "echo hi"
}
}

data "gocd_elastic_agent_profile" "sample_ec2" {
profile_id = "sample_ec2"
}
4 changes: 4 additions & 0 deletions examples/environment.tf
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@ resource "gocd_environment" "sample_environment" {
name = "TEST_ENV13"
value = "value_env13"
}
}

data "gocd_environment" "sample_environment" {
name = "sample_environment"
}
16 changes: 16 additions & 0 deletions examples/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,20 @@ output "encrypted_value" {

output "sample_config_repo" {
value = data.gocd_config_repository.sample_config_repo.material
}

output "password_file_config" {
value = data.gocd_auth_config.password_file_config.properties
}

output "ec2_cluster_profile" {
value = data.gocd_cluster_profile.ec2_cluster_profile.properties
}

output "sample_environment" {
value = data.gocd_environment.sample_environment.pipelines
}

output "sample_ec2" {
value = data.gocd_elastic_agent_profile.sample_ec2.properties
}
2 changes: 1 addition & 1 deletion internal/provider/cluster_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func resourceClusterProfile() *schema.Resource {
ForceNew: true,
Description: "the plugin identifier of the cluster profile.",
},
"properties": propertiesSchema(),
"properties": propertiesSchemaResource(),
"etag": {
Type: schema.TypeString,
Required: false,
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/config_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func resourceConfigRepository() *schema.Resource {
Description: "The plugin identifier of the cluster profile.",
},
"material": materialSchema(),
"configuration": propertiesSchema(),
"configuration": propertiesSchemaResource(),
"rules": {
Type: schema.TypeList,
Optional: true,
Expand Down
102 changes: 102 additions & 0 deletions internal/provider/data_auth_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
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 datasourceAuthConfig() *schema.Resource {
return &schema.Resource{
ReadContext: datasourceAuthConfigRead,
Schema: map[string]*schema.Schema{
"profile_id": {
Type: schema.TypeString,
Required: true,
Computed: false,
ForceNew: true,
Description: "The identifier of the elastic agent profile.",
},
"plugin_id": {
Type: schema.TypeString,
Computed: true,
Optional: true,
Required: false,
Description: "The plugin identifier of the cluster profile.",
},
"allow_only_known_users_to_login": {
Type: schema.TypeBool,
Computed: true,
Optional: true,
Required: false,
Description: "Allow only those users to login who have explicitly been added by an administrator.",
},
"properties": {
Type: schema.TypeList,
Computed: true,
Optional: true,
Description: "the list of configuration properties that represent the configuration of this profile.",
Elem: propertiesSchemaData(),
},
"etag": {
Type: schema.TypeString,
Computed: true,
Optional: true,
Description: "Etag used to track the authorisation configuration.",
},
},
}
}

func datasourceAuthConfigRead(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.GetAuthConfig(profileID)
if err != nil {
return diag.Errorf("getting authorization configuration %s errored with: %v", profileID, err)
}

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

if err = d.Set(utils.TerraformResourceAllowKnownUser, response.AllowOnlyKnownUsers); err != nil {
return diag.Errorf("setting '%s' errored with %v", err, utils.TerraformResourceAllowKnownUser)
}

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

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

if err = d.Set(utils.TerraformProperties, flattenedProperties); err != nil {
return diag.Errorf("setting '%s' errored with %v", err, utils.TerraformProperties)
}

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

d.SetId(id)

return nil
}
91 changes: 91 additions & 0 deletions internal/provider/data_cluster_profile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
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 datasourceClusterProfile() *schema.Resource {
return &schema.Resource{
ReadContext: datasourceClusterProfileRead,
Schema: map[string]*schema.Schema{
"profile_id": {
Type: schema.TypeString,
Required: true,
Computed: false,
ForceNew: true,
Description: "The identifier of the cluster profile.",
},
"plugin_id": {
Type: schema.TypeString,
Computed: true,
Optional: true,
Required: false,
Description: "The plugin identifier of the cluster profile.",
},
"properties": {
Type: schema.TypeList,
Computed: true,
Optional: true,
Description: "The list of configuration properties that represent the configuration of this profile.",
Elem: propertiesSchemaData(),
},
"etag": {
Type: schema.TypeString,
Computed: true,
Optional: true,
Description: "Etag used to track the cluster profile",
},
},
}
}

func datasourceClusterProfileRead(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.GetClusterProfile(profileID)
if err != nil {
return diag.Errorf("getting cluster profile %s errored with: %v", profileID, err)
}

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

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

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

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

if err = d.Set(utils.TerraformProperties, flattenedProperties); err != nil {
return diag.Errorf("setting '%s' errored with %v", err, utils.TerraformProperties)
}

d.SetId(id)

return nil
}
File renamed without changes.
91 changes: 91 additions & 0 deletions internal/provider/data_elastic_agent_profile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
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 datasourceElasticAgentProfile() *schema.Resource {
return &schema.Resource{
ReadContext: datasourceElasticAgentProfileRead,
Schema: map[string]*schema.Schema{
"profile_id": {
Type: schema.TypeString,
Required: true,
Computed: false,
ForceNew: true,
Description: "The identifier of the elastic agent profile.",
},
"cluster_profile_id": {
Type: schema.TypeString,
Computed: true,
Optional: true,
Required: false,
Description: "The identifier of the cluster profile to which current elastic agent profile belongs.",
},
"properties": {
Type: schema.TypeList,
Computed: true,
Optional: true,
Description: "The list of configuration properties that represent the configuration of this profile.",
Elem: propertiesSchemaData(),
},
"etag": {
Type: schema.TypeString,
Computed: true,
Optional: true,
Description: "Etag used to track the elastic agent profile.",
},
},
}
}

func datasourceElasticAgentProfileRead(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.GetElasticAgentProfile(profileID)
if err != nil {
return diag.Errorf("getting elastic agent profile %s errored with: %v", profileID, err)
}

if err = d.Set(utils.TerraformResourceClusterProfileID, response.ClusterProfileID); err != nil {
return diag.Errorf("setting '%s' errored with %v", err, utils.TerraformResourceClusterProfileID)
}

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

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

if err = d.Set(utils.TerraformProperties, flattenedProperties); err != nil {
return diag.Errorf("setting '%s' errored with %v", err, utils.TerraformProperties)
}

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

d.SetId(id)

return nil
}
Loading

0 comments on commit 316d1dc

Please sign in to comment.