Skip to content

Commit

Permalink
Add DatabaseConfig and WebServerConfig to Cloud Composer's Environmen…
Browse files Browse the repository at this point in the history
…tConfig
  • Loading branch information
anitakar committed Aug 21, 2020
1 parent 6ff42a9 commit 25ca0ec
Show file tree
Hide file tree
Showing 3 changed files with 589 additions and 207 deletions.
143 changes: 143 additions & 0 deletions third_party/terraform/resources/resource_composer_environment.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ var (
"config.0.private_environment_config",
<% unless version == "ga" -%>
"config.0.web_server_network_access_control",
<% end -%>
<% unless version == "ga" -%>
"config.0.database_config",
"config.0.web_server_config",
<% end -%>
}

Expand Down Expand Up @@ -393,6 +397,44 @@ func resourceComposerEnvironment() *schema.Resource {
},
},
},
<% end -%>
<% unless version == "ga" -%>
"database_config": {
Type: schema.TypeList,
Optional: true,
Computed: true,
AtLeastOneOf: composerConfigKeys,
MaxItems: 1,
Description: `The configuration of Cloud SQL instance that is used by the Apache Airflow software.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"machine_type": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: `Optional. Cloud SQL machine type used by Airflow database. It has to be one of: db-n1-standard-2, db-n1-standard-4, db-n1-standard-8 or db-n1-standard-16. If not specified, db-n1-standard-2 will be used.`,
},
},
},
},
"web_server_config": {
Type: schema.TypeList,
Optional: true,
Computed: true,
AtLeastOneOf: composerConfigKeys,
MaxItems: 1,
Description: `The configuration settings for the Airflow web server App Engine instance.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"machine_type": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: `Optional. Machine type on which Airflow web server is running. It has to be one of: composer-n1-webserver-2, composer-n1-webserver-4 or composer-n1-webserver-8. If not specified, composer-n1-webserver-2 will be used. Value custom is returned only in response, if Airflow web server parameters were manually changed to a non-standard values.`,
},
},
},
},
<% end -%>
"airflow_uri": {
Type: schema.TypeString,
Expand Down Expand Up @@ -631,6 +673,32 @@ func resourceComposerEnvironmentUpdate(d *schema.ResourceData, meta interface{})
}
d.SetPartial("config")
}

<% unless version == "ga" -%>
if d.HasChange("config.0.database_config") {
patchObj := &composer.Environment{Config: &composer.EnvironmentConfig{}}
if config != nil {
patchObj.Config.DatabaseConfig = config.DatabaseConfig
}
err = resourceComposerEnvironmentPatchField("config.databaseConfig", patchObj, d, tfConfig)
if err != nil {
return err
}
d.SetPartial("config")
}

if d.HasChange("config.0.web_server_config") {
patchObj := &composer.Environment{Config: &composer.EnvironmentConfig{}}
if config != nil {
patchObj.Config.WebServerConfig = config.WebServerConfig
}
err = resourceComposerEnvironmentPatchField("config.webServerConfig", patchObj, d, tfConfig)
if err != nil {
return err
}
d.SetPartial("config")
}
<% end -%>
}

if d.HasChange("labels") {
Expand Down Expand Up @@ -750,6 +818,10 @@ func flattenComposerEnvironmentConfig(envCfg *composer.EnvironmentConfig) interf
<% unless version == "ga" -%>
transformed["web_server_network_access_control"] = flattenComposerEnvironmentConfigWebServerNetworkAccessControl(envCfg.WebServerNetworkAccessControl)
<% end -%>
<% unless version == "ga" -%>
transformed["database_config"] = flattenComposerEnvironmentConfigDatabaseConfig(envCfg.DatabaseConfig)
transformed["web_server_config"] = flattenComposerEnvironmentConfigWebServerConfig(envCfg.WebServerConfig)
<% end -%>

return []interface{}{transformed}
}
Expand All @@ -775,8 +847,32 @@ func flattenComposerEnvironmentConfigWebServerNetworkAccessControl(accessControl

return []interface{}{webServerNetworkAccessControl}
}
<% end -%>

<% unless version == "ga" -%>
func flattenComposerEnvironmentConfigDatabaseConfig(databaseCfg *composer.DatabaseConfig) interface{} {
if databaseCfg == nil {
return nil
}

transformed := make(map[string]interface{})
transformed["machine_type"] = databaseCfg.MachineType

return []interface{}{transformed}
}

func flattenComposerEnvironmentConfigWebServerConfig(webServerCfg *composer.WebServerConfig) interface{} {
if webServerCfg == nil {
return nil
}

transformed := make(map[string]interface{})
transformed["machine_type"] = webServerCfg.MachineType

return []interface{}{transformed}
}
<% end -%>

func flattenComposerEnvironmentConfigPrivateEnvironmentConfig(envCfg *composer.PrivateEnvironmentConfig) interface{} {
if envCfg == nil {
return nil
Expand Down Expand Up @@ -890,6 +986,21 @@ func expandComposerEnvironmentConfig(v interface{}, d *schema.ResourceData, conf
}
transformed.WebServerNetworkAccessControl = transformedWebServerNetworkAccessControl

<% end -%>

<% unless version == "ga" -%>
transformedDatabaseConfig, err := expandComposerEnvironmentConfigDatabaseConfig(original["database_config"], d, config)
if err != nil {
return nil, err
}
transformed.DatabaseConfig = transformedDatabaseConfig

transformedWebServerConfig, err := expandComposerEnvironmentConfigWebServerConfig(original["web_server_config"], d, config)
if err != nil {
return nil, err
}
transformed.WebServerConfig = transformedWebServerConfig

<% end -%>
return transformed, nil
}
Expand Down Expand Up @@ -932,6 +1043,38 @@ func expandComposerEnvironmentConfigWebServerNetworkAccessControl(v interface{},
}

<% end -%>

<% unless version == "ga" -%>
func expandComposerEnvironmentConfigDatabaseConfig(v interface{}, d *schema.ResourceData, config *Config) (*composer.DatabaseConfig, error) {
l := v.([]interface{})
if len(l) == 0 || l[0] == nil {
return nil, nil
}
raw := l[0]
original := raw.(map[string]interface{})

transformed := &composer.DatabaseConfig{}
transformed.MachineType = original["machine_type"].(string)

return transformed, nil
}

func expandComposerEnvironmentConfigWebServerConfig(v interface{}, d *schema.ResourceData, config *Config) (*composer.WebServerConfig, error) {
l := v.([]interface{})
if len(l) == 0 || l[0] == nil {
return nil, nil
}
raw := l[0]
original := raw.(map[string]interface{})

transformed := &composer.WebServerConfig{}
transformed.MachineType = original["machine_type"].(string)

return transformed, nil
}

<% end -%>

func expandComposerEnvironmentConfigPrivateEnvironmentConfig(v interface{}, d *schema.ResourceData, config *Config) (*composer.PrivateEnvironmentConfig, error) {
l := v.([]interface{})
if len(l) == 0 {
Expand Down
Loading

0 comments on commit 25ca0ec

Please sign in to comment.