Skip to content

Commit

Permalink
Bumped dependencies; updated lifecycle implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
jbristowe committed Nov 6, 2020
1 parent 2f39592 commit 96698e0
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 72 deletions.
2 changes: 1 addition & 1 deletion go.mod
@@ -1,7 +1,7 @@
module github.com/OctopusDeploy/terraform-provider-octopusdeploy

require (
github.com/OctopusDeploy/go-octopusdeploy v1.6.1-0.20201104074852-ad2025ddd246
github.com/OctopusDeploy/go-octopusdeploy v1.6.1-0.20201106225944-de786ac1e947
github.com/agext/levenshtein v1.2.3 // indirect
github.com/apparentlymart/go-cidr v1.1.0 // indirect
github.com/aws/aws-sdk-go v1.34.13 // indirect
Expand Down
13 changes: 11 additions & 2 deletions octopusdeploy/data_source_lifecycle.go
Expand Up @@ -18,8 +18,17 @@ func dataSourceLifecycle() *schema.Resource {
Required: true,
Type: schema.TypeString,
},
constPhase: getPhasesSchema(),
constReleaseRetentionPolicy: getRetentionPeriodSchema(),
constPhase: {
Elem: phaseSchema(),
Optional: true,
Set: schema.HashResource(phaseSchema()),
Type: schema.TypeSet,
},
constReleaseRetentionPolicy: getRetentionPeriodSchema(),
constSpaceID: {
Type: schema.TypeString,
Computed: true,
},
constTentacleRetentionPolicy: getRetentionPeriodSchema(),
}

Expand Down
162 changes: 93 additions & 69 deletions octopusdeploy/resource_lifecycle.go
Expand Up @@ -22,8 +22,16 @@ func resourceLifecycle() *schema.Resource {
Required: true,
Type: schema.TypeString,
},
constPhase: getPhasesSchema(),
constReleaseRetentionPolicy: getRetentionPeriodSchema(),
constPhase: {
Elem: phaseSchema(),
Optional: true,
Type: schema.TypeList,
},
constReleaseRetentionPolicy: getRetentionPeriodSchema(),
constSpaceID: {
Type: schema.TypeString,
Computed: true,
},
constTentacleRetentionPolicy: getRetentionPeriodSchema(),
}

Expand Down Expand Up @@ -69,52 +77,50 @@ func getRetentionPeriodSchema() *schema.Schema {
}
}

func getPhasesSchema() *schema.Schema {
return &schema.Schema{
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
constAutomaticDeploymentTargets: {
Description: "Environment IDs in this phase that a release is automatically deployed to when it is eligible for this phase",
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
constID: {
Computed: true,
Type: schema.TypeString,
},
constName: {
Type: schema.TypeString,
Required: true,
},
constMinimumEnvironmentsBeforePromotion: {
Description: "The number of units required before a release can enter the next phase. If 0, all environments are required.",
Type: schema.TypeInt,
Optional: true,
Default: 0,
},
constIsOptionalPhase: {
Description: "If false a release must be deployed to this phase before it can be deployed to the next phase.",
Type: schema.TypeBool,
Optional: true,
Default: false,
},
constOptionalDeploymentTargets: {
Description: "Environment IDs in this phase that a release can be deployed to, but is not automatically deployed to",
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
constReleaseRetentionPolicy: getRetentionPeriodSchema(),
constTentacleRetentionPolicy: getRetentionPeriodSchema(),
func phaseSchema() *schema.Resource {
phaseSchema := map[string]*schema.Schema{
constAutomaticDeploymentTargets: {
Description: "Environment IDs in this phase that a release is automatically deployed to when it is eligible for this phase",
Elem: &schema.Schema{
Type: schema.TypeString,
},
Optional: true,
Type: schema.TypeList,
},
constID: {
Type: schema.TypeString,
Computed: true,
},
constIsOptionalPhase: {
Default: false,
Description: "If false a release must be deployed to this phase before it can be deployed to the next phase.",
Optional: true,
Type: schema.TypeBool,
},
constMinimumEnvironmentsBeforePromotion: {
Default: 0,
Description: "The number of units required before a release can enter the next phase. If 0, all environments are required.",
Optional: true,
Type: schema.TypeInt,
},
constName: {
Required: true,
Type: schema.TypeString,
},
constOptionalDeploymentTargets: {
Description: "Environment IDs in this phase that a release can be deployed to, but is not automatically deployed to",
Elem: &schema.Schema{
Type: schema.TypeString,
},
Optional: true,
Type: schema.TypeList,
},
constReleaseRetentionPolicy: getRetentionPeriodSchema(),
constTentacleRetentionPolicy: getRetentionPeriodSchema(),
}

return &schema.Resource{
Schema: phaseSchema,
}
}

Expand Down Expand Up @@ -143,6 +149,10 @@ func buildLifecycleResource(d *schema.ResourceData) *octopusdeploy.Lifecycle {
lifecycle.Description = v.(string)
}

if v, ok := d.GetOk(constSpaceID); ok {
lifecycle.SpaceID = v.(string)
}

releaseRetentionPolicy := getRetentionPeriod(d, constReleaseRetentionPolicy)
if releaseRetentionPolicy != nil {
lifecycle.ReleaseRetentionPolicy = *releaseRetentionPolicy
Expand All @@ -153,7 +163,7 @@ func buildLifecycleResource(d *schema.ResourceData) *octopusdeploy.Lifecycle {
lifecycle.TentacleRetentionPolicy = *tentacleRetentionPolicy
}

if attr, ok := d.GetOk(constPhase); ok {
if attr, ok := d.GetOk(constPhases); ok {
tfPhases := attr.([]interface{})

for _, tfPhase := range tfPhases {
Expand All @@ -172,8 +182,9 @@ func getRetentionPeriod(d *schema.ResourceData, key string) *octopusdeploy.Reten
if len(retentionPeriod) == 1 {
tfRetentionItem := retentionPeriod[0].(map[string]interface{})
retention := octopusdeploy.RetentionPeriod{
Unit: tfRetentionItem[constUnit].(string),
QuantityToKeep: int32(tfRetentionItem[constQuantityToKeep].(int)),
QuantityToKeep: int32(tfRetentionItem[constQuantityToKeep].(int)),
ShouldKeepForever: tfRetentionItem[constShouldKeepForever].(bool),
Unit: tfRetentionItem[constUnit].(string),
}
return &retention
}
Expand All @@ -184,10 +195,10 @@ func getRetentionPeriod(d *schema.ResourceData, key string) *octopusdeploy.Reten

func buildPhaseResource(tfPhase map[string]interface{}) octopusdeploy.Phase {
phase := octopusdeploy.Phase{
Name: tfPhase[constName].(string),
MinimumEnvironmentsBeforePromotion: int32(tfPhase[constMinimumEnvironmentsBeforePromotion].(int)),
IsOptionalPhase: tfPhase[constIsOptionalPhase].(bool),
AutomaticDeploymentTargets: getSliceFromTerraformTypeList(tfPhase[constAutomaticDeploymentTargets]),
IsOptionalPhase: tfPhase[constIsOptionalPhase].(bool),
MinimumEnvironmentsBeforePromotion: int32(tfPhase[constMinimumEnvironmentsBeforePromotion].(int)),
Name: tfPhase[constName].(string),
OptionalDeploymentTargets: getSliceFromTerraformTypeList(tfPhase[constOptionalDeploymentTargets]),
}

Expand Down Expand Up @@ -237,35 +248,48 @@ func resourceLifecycleDelete(ctx context.Context, d *schema.ResourceData, m inte
return nil
}

func flattenPhase(p octopusdeploy.Phase) []interface{} {
phase := make(map[string]interface{})
phase[constAutomaticDeploymentTargets] = p.AutomaticDeploymentTargets
phase[constID] = p.ID
phase[constIsOptionalPhase] = p.IsOptionalPhase
phase[constMinimumEnvironmentsBeforePromotion] = p.MinimumEnvironmentsBeforePromotion
phase[constName] = p.Name
phase[constOptionalDeploymentTargets] = p.OptionalDeploymentTargets
phase[constReleaseRetentionPolicy] = p.ReleaseRetentionPolicy
phase[constTentacleRetentionPolicy] = p.TentacleRetentionPolicy
return []interface{}{phase}
func flattenStringArray(values []string) []interface{} {
s := make([]interface{}, len(values))
for i, v := range values {
s[i] = v
}
return s
}

func flattenPhases(phases []octopusdeploy.Phase) []interface{} {
flattenedPhases := make([]interface{}, 0)
for _, phase := range phases {
p := make(map[string]interface{})
p[constAutomaticDeploymentTargets] = flattenStringArray(phase.AutomaticDeploymentTargets)
p[constID] = phase.ID
p[constIsOptionalPhase] = phase.IsOptionalPhase
p[constMinimumEnvironmentsBeforePromotion] = int(phase.MinimumEnvironmentsBeforePromotion)
p[constName] = phase.Name
p[constOptionalDeploymentTargets] = flattenStringArray(phase.OptionalDeploymentTargets)
if phase.ReleaseRetentionPolicy != nil {
p[constReleaseRetentionPolicy] = flattenRetentionPeriod(*phase.ReleaseRetentionPolicy)
}
if phase.TentacleRetentionPolicy != nil {
p[constTentacleRetentionPolicy] = flattenRetentionPeriod(*phase.TentacleRetentionPolicy)
}
flattenedPhases = append(flattenedPhases, p)
}
return flattenedPhases
}

func flattenRetentionPeriod(r octopusdeploy.RetentionPeriod) []interface{} {
retentionPeriod := make(map[string]interface{})
retentionPeriod[constUnit] = r.Unit
retentionPeriod[constQuantityToKeep] = r.QuantityToKeep
retentionPeriod[constQuantityToKeep] = int(r.QuantityToKeep)
retentionPeriod[constShouldKeepForever] = r.ShouldKeepForever
return []interface{}{retentionPeriod}
}

func flattenLifecycle(ctx context.Context, d *schema.ResourceData, lifecycle *octopusdeploy.Lifecycle) {
d.Set(constDescription, lifecycle.Description)
d.Set(constName, lifecycle.Name)

for _, phase := range lifecycle.Phases {
d.Set(constPhase, flattenPhase(phase))
}

d.Set(constPhase, flattenPhases(lifecycle.Phases))
d.Set(constSpaceID, lifecycle.SpaceID)
d.Set(constReleaseRetentionPolicy, flattenRetentionPeriod(lifecycle.ReleaseRetentionPolicy))
d.Set(constTentacleRetentionPolicy, flattenRetentionPeriod(lifecycle.TentacleRetentionPolicy))

Expand Down

0 comments on commit 96698e0

Please sign in to comment.