Skip to content

Commit

Permalink
add lifecycle_config to dataproc_cluster.cluster_config (#2926)
Browse files Browse the repository at this point in the history
Merged PR #2926.
  • Loading branch information
megan07 authored and modular-magician committed Jan 8, 2020
1 parent 1f9be95 commit a72a112
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 4 deletions.
2 changes: 1 addition & 1 deletion build/terraform
2 changes: 1 addition & 1 deletion build/terraform-beta
2 changes: 1 addition & 1 deletion build/terraform-mapper
101 changes: 100 additions & 1 deletion third_party/terraform/resources/resource_dataproc_cluster.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ var (
"cluster_config.0.initialization_action",
"cluster_config.0.encryption_config",
"cluster_config.0.autoscaling_config",
<% unless version == 'ga' -%>
"cluster_config.0.lifecycle_config",
<% end -%>
}
)

Expand Down Expand Up @@ -508,6 +511,42 @@ by Dataproc`,
},
},
},
<% unless version == 'ga' -%>
"lifecycle_config": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
AtLeastOneOf: clusterConfigKeys,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"idle_delete_ttl": {
Type: schema.TypeString,
Optional: true,
AtLeastOneOf: []string{
"cluster_config.0.lifecycle_config.0.idle_delete_ttl",
"cluster_config.0.lifecycle_config.0.auto_delete_time",
},
},
"idle_start_time": {
Type: schema.TypeString,
Computed: true,
},
// the API also has the auto_delete_ttl option in its request, however,
// the value is not returned in the response, rather the auto_delete_time
// after calculating ttl with the update time is returned, thus, for now
// we will only allow auto_delete_time to updated.
"auto_delete_time": {
Type: schema.TypeString,
Optional: true,
AtLeastOneOf: []string{
"cluster_config.0.lifecycle_config.0.idle_delete_ttl",
"cluster_config.0.lifecycle_config.0.auto_delete_time",
},
},
},
},
},
<% end -%>
},
},
},
Expand Down Expand Up @@ -702,7 +741,6 @@ func resourceDataprocClusterCreate(d *schema.ResourceData, meta interface{}) err

log.Printf("[INFO] Dataproc cluster %s has been created", cluster.ClusterName)
return resourceDataprocClusterRead(d, meta)

}

func expandClusterConfig(d *schema.ResourceData, config *Config) (*dataproc.ClusterConfig, error) {
Expand Down Expand Up @@ -749,6 +787,12 @@ func expandClusterConfig(d *schema.ResourceData, config *Config) (*dataproc.Clus
conf.AutoscalingConfig = expandAutoscalingConfig(cfg)
}

<% unless version == 'ga' -%>
if cfg, ok := configOptions(d, "cluster_config.0.lifecycle_config"); ok {
conf.LifecycleConfig = expandLifecycleConfig(cfg)
}
<% end -%>

if cfg, ok := configOptions(d, "cluster_config.0.master_config"); ok {
log.Println("[INFO] got master_config")
conf.MasterConfig = expandInstanceGroupConfig(cfg)
Expand Down Expand Up @@ -918,6 +962,19 @@ func expandAutoscalingConfig(cfg map[string]interface{}) *dataproc.AutoscalingCo
return conf
}

<% unless version == 'ga' -%>
func expandLifecycleConfig(cfg map[string]interface{}) *dataproc.LifecycleConfig {
conf := &dataproc.LifecycleConfig{}
if v, ok := cfg["idle_delete_ttl"]; ok {
conf.IdleDeleteTtl = v.(string)
}
if v, ok := cfg["auto_delete_time"]; ok {
conf.AutoDeleteTime = v.(string)
}
return conf
}
<% end -%>

func expandInitializationActions(v interface{}) []*dataproc.NodeInitializationAction {
actionList := v.([]interface{})

Expand Down Expand Up @@ -1064,6 +1121,30 @@ func resourceDataprocClusterUpdate(d *schema.ResourceData, meta interface{}) err
updMask = append(updMask, "config.secondary_worker_config.num_instances")
}

<% unless version == 'ga' -%>
if d.HasChange("cluster_config.0.lifecycle_config.0.idle_delete_ttl") {
idleDeleteTtl := d.Get("cluster_config.0.lifecycle_config.0.idle_delete_ttl").(string)
cluster.Config.LifecycleConfig = &dataproc.LifecycleConfig{
IdleDeleteTtl: idleDeleteTtl,
}

updMask = append(updMask, "config.lifecycle_config.idle_delete_ttl")
}

if d.HasChange("cluster_config.0.lifecycle_config.0.auto_delete_time") {
desiredDeleteTime := d.Get("cluster_config.0.lifecycle_config.0.auto_delete_time").(string)
if cluster.Config.LifecycleConfig != nil {
cluster.Config.LifecycleConfig.AutoDeleteTime = desiredDeleteTime
} else {
cluster.Config.LifecycleConfig = &dataproc.LifecycleConfig{
AutoDeleteTime: desiredDeleteTime,
}
}

updMask = append(updMask, "config.lifecycle_config.auto_delete_time")
}
<% end -%>

if len(updMask) > 0 {
patch := config.clientDataprocBeta.Projects.Regions.Clusters.Patch(
project, region, clusterName, cluster)
Expand Down Expand Up @@ -1132,6 +1213,9 @@ func flattenClusterConfig(d *schema.ResourceData, cfg *dataproc.ClusterConfig) (
"preemptible_worker_config": flattenPreemptibleInstanceGroupConfig(d, cfg.SecondaryWorkerConfig),
"encryption_config": flattenEncryptionConfig(d, cfg.EncryptionConfig),
"autoscaling_config": flattenAutoscalingConfig(d, cfg.AutoscalingConfig),
<% unless version == 'ga' -%>
"lifecycle_config": flattenLifecycleConfig(d, cfg.LifecycleConfig),
<% end -%>
}

if len(cfg.InitializationActions) > 0 {
Expand Down Expand Up @@ -1212,6 +1296,21 @@ func flattenAutoscalingConfig(d *schema.ResourceData, ec *dataproc.AutoscalingCo
return []map[string]interface{}{data}
}

<% unless version == 'ga' -%>
func flattenLifecycleConfig(d *schema.ResourceData, lc *dataproc.LifecycleConfig) []map[string]interface{} {
if lc == nil {
return nil
}

data := map[string]interface{}{
"idle_delete_ttl": lc.IdleDeleteTtl,
"auto_delete_time": lc.AutoDeleteTime,
}

return []map[string]interface{}{data}
}
<% end -%>

func flattenAccelerators(accelerators []*dataproc.AcceleratorConfig) interface{} {
acceleratorsTypeSet := schema.NewSet(schema.HashResource(acceleratorsSchema()), []interface{}{})
for _, accelerator := range accelerators {
Expand Down
90 changes: 90 additions & 0 deletions third_party/terraform/tests/resource_dataproc_cluster_test.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"regexp"
"strconv"
"testing"
"time"

"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
Expand Down Expand Up @@ -529,6 +530,63 @@ func TestAccDataprocCluster_withOptionalComponents(t *testing.T) {
})
}

<% unless version == 'ga' -%>
func TestAccDataprocCluster_withLifecycleConfigIdleDeleteTtl(t *testing.T) {
t.Parallel()

rnd := acctest.RandString(10)
var cluster dataproc.Cluster
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckDataprocClusterDestroy(),
Steps: []resource.TestStep{
{
Config: testAccDataprocCluster_withLifecycleConfigIdleDeleteTtl(rnd, "600s"),
Check: resource.ComposeTestCheckFunc(
testAccCheckDataprocClusterExists("google_dataproc_cluster.with_lifecycle_config", &cluster),
),
},
{
Config: testAccDataprocCluster_withLifecycleConfigIdleDeleteTtl(rnd, "610s"),
Check: resource.ComposeTestCheckFunc(
testAccCheckDataprocClusterExists("google_dataproc_cluster.with_lifecycle_config", &cluster),
),
},
},
})
}

func TestAccDataprocCluster_withLifecycleConfigAutoDeletion(t *testing.T) {
t.Parallel()

rnd := acctest.RandString(10)
now := time.Now()
fmtString := "2006-01-02T15:04:05.072Z"

var cluster dataproc.Cluster
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckDataprocClusterDestroy(),
Steps: []resource.TestStep{
{
Config: testAccDataprocCluster_withLifecycleConfigAutoDeletionTime(rnd, now.Add(time.Hour * 10).Format(fmtString)),
Check: resource.ComposeTestCheckFunc(
testAccCheckDataprocClusterExists("google_dataproc_cluster.with_lifecycle_config", &cluster),
),
},
{
Config: testAccDataprocCluster_withLifecycleConfigAutoDeletionTime(rnd, now.Add(time.Hour * 20).Format(fmtString)),
Check: resource.ComposeTestCheckFunc(
testAccCheckDataprocClusterExists("google_dataproc_cluster.with_lifecycle_config", &cluster),
),
},
},
})
}
<% end -%>

func TestAccDataprocCluster_withLabels(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -1202,6 +1260,38 @@ resource "google_dataproc_cluster" "with_opt_components" {
`, rnd)
}

<% unless version == 'ga' -%>
func testAccDataprocCluster_withLifecycleConfigIdleDeleteTtl(rnd, tm string) string {
return fmt.Sprintf(`
resource "google_dataproc_cluster" "with_lifecycle_config" {
name = "dproc-cluster-test-%s"
region = "us-central1"

cluster_config {
lifecycle_config {
idle_delete_ttl = "%s"
}
}
}
`, rnd, tm)
}

func testAccDataprocCluster_withLifecycleConfigAutoDeletionTime(rnd, tm string) string {
return fmt.Sprintf(`
resource "google_dataproc_cluster" "with_lifecycle_config" {
name = "dproc-cluster-test-%s"
region = "us-central1"

cluster_config {
lifecycle_config {
auto_delete_time = "%s"
}
}
}
`, rnd, tm)
}
<% end -%>

func testAccDataprocCluster_withServiceAcc(sa string, rnd string) string {
return fmt.Sprintf(`
resource "google_service_account" "service_account" {
Expand Down

0 comments on commit a72a112

Please sign in to comment.