Skip to content

Commit

Permalink
Guard against nil empty typelists.
Browse files Browse the repository at this point in the history
  • Loading branch information
rileykarson committed Oct 11, 2018
1 parent 3f40d25 commit f0e8e26
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 36 deletions.
11 changes: 4 additions & 7 deletions google/resource_cloudfunctions_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,11 +586,11 @@ func resourceCloudFunctionsDestroy(d *schema.ResourceData, meta interface{}) err
}

func expandEventTrigger(configured []interface{}, project string) *cloudfunctions.EventTrigger {
if len(configured) == 0 {
if len(configured) == 0 || configured[0] == nil {
return nil
}

if data, ok := configured[0].(map[string]interface{}); ok {
data := configured[0].(map[string]interface{})
eventType := data["event_type"].(string)
shape := ""
switch {
Expand All @@ -605,9 +605,6 @@ func expandEventTrigger(configured []interface{}, project string) *cloudfunction
Resource: fmt.Sprintf(shape, project, data["resource"].(string)),
FailurePolicy: expandFailurePolicy(data["failure_policy"].([]interface{})),
}
}

return nil
}

func flattenEventTrigger(eventTrigger *cloudfunctions.EventTrigger) []map[string]interface{} {
Expand All @@ -626,11 +623,11 @@ func flattenEventTrigger(eventTrigger *cloudfunctions.EventTrigger) []map[string
}

func expandFailurePolicy(configured []interface{}) *cloudfunctions.FailurePolicy {
if len(configured) == 0 {
if len(configured) == 0 || configured[0] == nil {
return &cloudfunctions.FailurePolicy{}
}

if data, ok := configured[0].(map[string]interface{}); ok && data["retry"].(bool) {
if data := configured[0].(map[string]interface{}); data["retry"].(bool) {
return &cloudfunctions.FailurePolicy{
Retry: &cloudfunctions.Retry{},
}
Expand Down
22 changes: 10 additions & 12 deletions google/resource_compute_backend_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,19 +399,17 @@ func resourceComputeBackendServiceDelete(d *schema.ResourceData, meta interface{
}

func expandIap(configured []interface{}) *computeBeta.BackendServiceIAP {
if len(configured) == 0 {
if len(configured) == 0 || configured[0] == nil {
return nil
}
if data, ok := configured[0].(map[string]interface{}); ok {
return &computeBeta.BackendServiceIAP{
Enabled: true,
Oauth2ClientId: data["oauth2_client_id"].(string),
Oauth2ClientSecret: data["oauth2_client_secret"].(string),
ForceSendFields: []string{"Enabled", "Oauth2ClientId", "Oauth2ClientSecret"},
}
}

return nil
data := configured[0].(map[string]interface{})
return &computeBeta.BackendServiceIAP{
Enabled: true,
Oauth2ClientId: data["oauth2_client_id"].(string),
Oauth2ClientSecret: data["oauth2_client_secret"].(string),
ForceSendFields: []string{"Enabled", "Oauth2ClientId", "Oauth2ClientSecret"},
}
}

func flattenIap(iap *computeBeta.BackendServiceIAP) []map[string]interface{} {
Expand Down Expand Up @@ -591,11 +589,11 @@ func expandBackendService(d *schema.ResourceData) (*computeBeta.BackendService,
}

func expandCdnPolicy(configured []interface{}) *computeBeta.BackendServiceCdnPolicy {
if len(configured) == 0 {
if len(configured) == 0 || configured[0] == nil {
return nil
}
data := configured[0].(map[string]interface{})

data := configured[0].(map[string]interface{})
ckp := data["cache_key_policy"].([]interface{})
if len(ckp) == 0 {
return nil
Expand Down
6 changes: 4 additions & 2 deletions google/resource_compute_security_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,9 +317,10 @@ func expandSecurityPolicyRule(raw interface{}) *compute.SecurityPolicyRule {
}

func expandSecurityPolicyMatch(configured []interface{}) *compute.SecurityPolicyRuleMatcher {
if len(configured) == 0 {
if len(configured) == 0 || configured[0] == nil {
return nil
}

data := configured[0].(map[string]interface{})
return &compute.SecurityPolicyRuleMatcher{
VersionedExpr: data["versioned_expr"].(string),
Expand All @@ -328,9 +329,10 @@ func expandSecurityPolicyMatch(configured []interface{}) *compute.SecurityPolicy
}

func expandSecurityPolicyMatchConfig(configured []interface{}) *compute.SecurityPolicyRuleMatcherConfig {
if len(configured) == 0 {
if len(configured) == 0 || configured[0] == nil {
return nil
}

data := configured[0].(map[string]interface{})
return &compute.SecurityPolicyRuleMatcherConfig{
SrcIpRanges: convertStringArr(data["src_ip_ranges"].(*schema.Set).List()),
Expand Down
15 changes: 10 additions & 5 deletions google/resource_container_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -1363,9 +1363,10 @@ func getInstanceGroupUrlsFromManagerUrls(config *Config, igmUrls []string) ([]st

func expandClusterAddonsConfig(configured interface{}) *containerBeta.AddonsConfig {
l := configured.([]interface{})
if len(l) == 0 {
if len(l) == 0 || l[0] == nil {
return nil
}

config := l[0].(map[string]interface{})
ac := &containerBeta.AddonsConfig{}

Expand Down Expand Up @@ -1406,9 +1407,10 @@ func expandClusterAddonsConfig(configured interface{}) *containerBeta.AddonsConf

func expandIPAllocationPolicy(configured interface{}) *containerBeta.IPAllocationPolicy {
l := configured.([]interface{})
if len(l) == 0 {
if len(l) == 0 || l[0] == nil {
return nil
}

config := l[0].(map[string]interface{})

return &containerBeta.IPAllocationPolicy{
Expand All @@ -1427,9 +1429,10 @@ func expandIPAllocationPolicy(configured interface{}) *containerBeta.IPAllocatio

func expandMaintenancePolicy(configured interface{}) *containerBeta.MaintenancePolicy {
l := configured.([]interface{})
if len(l) == 0 {
if len(l) == 0 || l[0] == nil {
return nil
}

maintenancePolicy := l[0].(map[string]interface{})
dailyMaintenanceWindow := maintenancePolicy["daily_maintenance_window"].([]interface{})[0].(map[string]interface{})
startTime := dailyMaintenanceWindow["start_time"].(string)
Expand All @@ -1444,9 +1447,10 @@ func expandMaintenancePolicy(configured interface{}) *containerBeta.MaintenanceP

func expandMasterAuth(configured interface{}) *containerBeta.MasterAuth {
l := configured.([]interface{})
if len(l) == 0 {
if len(l) == 0 || l[0] == nil {
return nil
}

masterAuth := l[0].(map[string]interface{})
result := &containerBeta.MasterAuth{
Username: masterAuth["username"].(string),
Expand Down Expand Up @@ -1507,9 +1511,10 @@ func expandNetworkPolicy(configured interface{}) *containerBeta.NetworkPolicy {

func expandPodSecurityPolicyConfig(configured interface{}) *containerBeta.PodSecurityPolicyConfig {
l := configured.([]interface{})
if len(l) == 0 {
if len(l) == 0 || l[0] == nil {
return nil
}

config := l[0].(map[string]interface{})
return &containerBeta.PodSecurityPolicyConfig{
Enabled: config["enabled"].(bool),
Expand Down
6 changes: 3 additions & 3 deletions google/resource_google_organization_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ func flattenRestoreOrganizationPolicy(restore_policy *cloudresourcemanager.Resto
}

func expandBooleanOrganizationPolicy(configured []interface{}) *cloudresourcemanager.BooleanPolicy {
if len(configured) == 0 {
if len(configured) == 0 || configured[0] == nil {
return nil
}

Expand All @@ -274,7 +274,7 @@ func expandBooleanOrganizationPolicy(configured []interface{}) *cloudresourceman
}

func expandRestoreOrganizationPolicy(configured []interface{}) (*cloudresourcemanager.RestoreDefault, error) {
if len(configured) == 0 {
if len(configured) == 0 || configured[0] == nil {
return nil, nil
}

Expand Down Expand Up @@ -321,7 +321,7 @@ func flattenListOrganizationPolicy(policy *cloudresourcemanager.ListPolicy) []ma
}

func expandListOrganizationPolicy(configured []interface{}) (*cloudresourcemanager.ListPolicy, error) {
if len(configured) == 0 {
if len(configured) == 0 || configured[0] == nil {
return nil, nil
}

Expand Down
2 changes: 1 addition & 1 deletion google/resource_pubsub_subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func flattenPubsubSubscriptionPushConfig(pushConfig *pubsub.PushConfig) []map[st
}

func expandPubsubSubscriptionPushConfig(configured []interface{}) *pubsub.PushConfig {
if len(configured) == 0 {
if len(configured) == 0 || configured[0] == nil {
// An empty `pushConfig` indicates that the Pub/Sub system should stop pushing messages
// from the given subscription and allow messages to be pulled and acknowledged.
return &pubsub.PushConfig{}
Expand Down
12 changes: 6 additions & 6 deletions google/resource_sql_database_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ func resourceSqlDatabaseInstanceCreate(d *schema.ResourceData, meta interface{})
}

func expandSqlDatabaseInstanceSettings(configured []interface{}, secondGen bool) *sqladmin.Settings {
if len(configured) == 0 {
if len(configured) == 0 || configured[0] == nil {
return nil
}

Expand Down Expand Up @@ -556,7 +556,7 @@ func expandSqlDatabaseInstanceSettings(configured []interface{}, secondGen bool)
}

func expandReplicaConfiguration(configured []interface{}) *sqladmin.ReplicaConfiguration {
if len(configured) == 0 {
if len(configured) == 0 || configured[0] == nil {
return nil
}

Expand All @@ -582,7 +582,7 @@ func expandReplicaConfiguration(configured []interface{}) *sqladmin.ReplicaConfi
}

func expandMaintenanceWindow(configured []interface{}) *sqladmin.MaintenanceWindow {
if len(configured) == 0 {
if len(configured) == 0 || configured[0] == nil {
return nil
}

Expand All @@ -595,7 +595,7 @@ func expandMaintenanceWindow(configured []interface{}) *sqladmin.MaintenanceWind
}

func expandLocationPreference(configured []interface{}) *sqladmin.LocationPreference {
if len(configured) == 0 {
if len(configured) == 0 || configured[0] == nil {
return nil
}

Expand All @@ -607,7 +607,7 @@ func expandLocationPreference(configured []interface{}) *sqladmin.LocationPrefer
}

func expandIpConfiguration(configured []interface{}) *sqladmin.IpConfiguration {
if len(configured) == 0 {
if len(configured) == 0 || configured[0] == nil {
return nil
}

Expand Down Expand Up @@ -654,7 +654,7 @@ func expandDatabaseFlags(configured []interface{}) []*sqladmin.DatabaseFlags {
}

func expandBackupConfiguration(configured []interface{}) *sqladmin.BackupConfiguration {
if len(configured) == 0 {
if len(configured) == 0 || configured[0] == nil {
return nil
}

Expand Down

0 comments on commit f0e8e26

Please sign in to comment.