-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for resource pipeline_group
- Loading branch information
1 parent
ac1b5d8
commit 9a3c62d
Showing
9 changed files
with
198 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
resource "gocd_pipeline_group" "sample_group_2" { | ||
name = "sample-group-2" | ||
pipelines = ["helm-images", "helm-drift"] | ||
authorization { | ||
view { | ||
users = ["nikhil"] | ||
roles = ["sample"] | ||
} | ||
operate { | ||
users = ["nikhil"] | ||
roles = ["sample"] | ||
} | ||
admins { | ||
users = ["nikhil"] | ||
roles = ["sample"] | ||
} | ||
} | ||
} | ||
|
||
data "gocd_pipeline_group" "sample_group" { | ||
group_id = "sample-group" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
package provider | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"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 resourcePipelineGroup() *schema.Resource { | ||
return &schema.Resource{ | ||
CreateContext: resourcePipelineGroupCreate, | ||
ReadContext: resourcePipelineGroupRead, | ||
UpdateContext: resourcePipelineGroupUpdate, | ||
DeleteContext: resourcePipelineGroupDelete, | ||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
Computed: false, | ||
ForceNew: true, | ||
Description: "Name of the pipeline group to be created or updated.", | ||
}, | ||
"pipelines": { | ||
Type: schema.TypeList, | ||
Optional: true, | ||
ForceNew: false, | ||
Description: "List of pipelines to be associated with pipeline group.", | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
}, | ||
"authorization": { | ||
Type: schema.TypeSet, | ||
Optional: true, | ||
ForceNew: false, | ||
Description: "The authorization configuration for the pipeline group.", | ||
Elem: authConfigSchema(), | ||
}, | ||
"etag": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Optional: true, | ||
Description: "Etag used to track the pipeline group.", | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourcePipelineGroupCreate(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.TerraformResourceName)) | ||
id = resourceID | ||
} | ||
|
||
cfg := gocd.PipelineGroup{ | ||
Name: id, | ||
Authorization: getPipelineGroupAuthorizationConfig(d.Get(utils.TerraformResourceAuthorization)), | ||
} | ||
|
||
out, err := json.MarshalIndent(cfg, " ", " ") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
log.Printf("PipelineGroup: %s", string(out)) | ||
|
||
if err := defaultConfig.CreatePipelineGroup(cfg); err != nil { | ||
return diag.Errorf("creating pipeline group '%s' errored with %v", cfg.Name, err) | ||
} | ||
|
||
d.SetId(id) | ||
|
||
return resourcePipelineGroupRead(ctx, d, meta) | ||
} | ||
|
||
func resourcePipelineGroupRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
defaultConfig := meta.(gocd.GoCd) | ||
|
||
name := utils.String(d.Get(utils.TerraformResourceName)) | ||
response, err := defaultConfig.GetPipelineGroup(name) | ||
if err != nil { | ||
return diag.Errorf("getting pipeline group '%s' errored with: %v", name, err) | ||
} | ||
|
||
if err = d.Set(utils.TerraformResourceEtag, response.ETAG); err != nil { | ||
return diag.Errorf(settingAttrErrorTmp, utils.TerraformResourceEtag, err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func resourcePipelineGroupUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
defaultConfig := meta.(gocd.GoCd) | ||
|
||
if !d.HasChange(utils.TerraformResourceAuthorization) && !d.HasChange(utils.TerraformResourcePipelines) { | ||
log.Printf("nothing to update so skipping") | ||
|
||
return nil | ||
} | ||
|
||
cfg := gocd.PipelineGroup{ | ||
Name: utils.String(d.Get(utils.TerraformResourceName)), | ||
Pipelines: getPipelines(d.Get(utils.TerraformResourcePipelines)), | ||
Authorization: getPipelineGroupAuthorizationConfig(d.Get(utils.TerraformResourceAuthorization)), | ||
ETAG: utils.String(d.Get(utils.TerraformResourceEtag)), | ||
} | ||
|
||
if _, err := defaultConfig.UpdatePipelineGroup(cfg); err != nil { | ||
return diag.Errorf("updating pipeline group '%s' errored with: %v", cfg.Name, err) | ||
} | ||
|
||
return resourcePipelineGroupRead(ctx, d, meta) | ||
} | ||
|
||
func resourcePipelineGroupDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
defaultConfig := meta.(gocd.GoCd) | ||
|
||
if id := d.Id(); len(id) == 0 { | ||
return diag.Errorf("resource with the ID '%s' not found", id) | ||
} | ||
|
||
profileID := utils.String(d.Get(utils.TerraformResourceName)) | ||
|
||
err := defaultConfig.DeletePipelineGroup(profileID) | ||
if err != nil { | ||
return diag.Errorf("deleting pipeline group errored with: %v", err) | ||
} | ||
|
||
d.SetId("") | ||
|
||
return nil | ||
} | ||
|
||
func getPipelineGroupAuthorizationConfig(authConfig interface{}) gocd.PipelineGroupAuthorizationConfig { | ||
var authoRisationConfig gocd.PipelineGroupAuthorizationConfig | ||
flattenedAuthConfig := authConfig.(*schema.Set).List()[0].(map[string]interface{}) | ||
flattenedView := flattenedAuthConfig[utils.TerraformResourceView].(*schema.Set).List()[0].(map[string]interface{}) | ||
flattenedOperate := flattenedAuthConfig[utils.TerraformResourceOperate].(*schema.Set).List()[0].(map[string]interface{}) | ||
flattenedAdmins := flattenedAuthConfig[utils.TerraformResourceAdmins].(*schema.Set).List()[0].(map[string]interface{}) | ||
|
||
authoRisationConfig.Admins = gocd.AuthorizationConfig{ | ||
Roles: utils.GetSlice(flattenedAdmins[utils.TerraformResourceRoles].([]interface{})), | ||
Users: utils.GetSlice(flattenedAdmins[utils.TerraformResourceUsers].([]interface{})), | ||
} | ||
authoRisationConfig.View = gocd.AuthorizationConfig{ | ||
Roles: utils.GetSlice(flattenedView[utils.TerraformResourceRoles].([]interface{})), | ||
Users: utils.GetSlice(flattenedView[utils.TerraformResourceUsers].([]interface{})), | ||
} | ||
authoRisationConfig.Operate = gocd.AuthorizationConfig{ | ||
Roles: utils.GetSlice(flattenedOperate[utils.TerraformResourceRoles].([]interface{})), | ||
Users: utils.GetSlice(flattenedOperate[utils.TerraformResourceUsers].([]interface{})), | ||
} | ||
|
||
return authoRisationConfig | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters