Skip to content

Commit

Permalink
Cleanup BackendService after generation, add omitted fields.
Browse files Browse the repository at this point in the history
  • Loading branch information
rileykarson committed Apr 3, 2019
1 parent 56413c0 commit 5011895
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 195 deletions.
4 changes: 3 additions & 1 deletion products/compute/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,9 @@ objects:
description: |
Indicates whether the backend service will be used with internal or
external load balancing. A backend service created for one type of
load balancing cannot be used with the other.
load balancing cannot be used with the other. One of `INTERNAL` or
`EXTERNAL`. Defaults to `EXTERNAL`.
default_value: :EXTERNAL
values:
- :INTERNAL
- :EXTERNAL
Expand Down
7 changes: 1 addition & 6 deletions products/compute/terraform.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ overrides: !ruby/object:Overrides::ResourceOverrides
backend_service_name: "backend-service"
http_health_check_name: "health-check"
custom_code: !ruby/object:Provider::Terraform::CustomCode
constants: 'templates/terraform/constants/backend_service.go.erb'
encoder: 'templates/terraform/encoders/backend_service.go.erb'
decoder: 'templates/terraform/decoders/backend_service.go.erb'
post_create: 'templates/terraform/post_create/compute_backend_service_security_policy.go.erb'
Expand All @@ -195,9 +196,6 @@ overrides: !ruby/object:Overrides::ResourceOverrides
is_set: true
cdnPolicy.cacheKeyPolicy.queryStringBlacklist: !ruby/object:Overrides::Terraform::PropertyOverride
is_set: true
# TODO(rileykarson): Add this- exclude for now to make the PR easier to review.
cdnPolicy.signedUrlCacheMaxAgeSec: !ruby/object:Overrides::Terraform::PropertyOverride
exclude: true
connectionDraining: !ruby/object:Overrides::Terraform::PropertyOverride
flatten_object: true
connectionDraining.drainingTimeoutSec: !ruby/object:Overrides::Terraform::PropertyOverride
Expand All @@ -222,9 +220,6 @@ overrides: !ruby/object:Overrides::ResourceOverrides
sensitive: true
id: !ruby/object:Overrides::Terraform::PropertyOverride
exclude: true
# TODO(rileykarson): Add this- exclude for now to make the PR easier to review.
loadBalancingScheme: !ruby/object:Overrides::Terraform::PropertyOverride
exclude: true
portName: !ruby/object:Overrides::Terraform::PropertyOverride
default_from_api: true
protocol: !ruby/object:Overrides::Terraform::PropertyOverride
Expand Down
Original file line number Diff line number Diff line change
@@ -1,96 +1,17 @@
package google

import (
"fmt"
"log"
"strconv"
"strings"

"bytes"
"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/terraform"
)

func resourceComputeBackendServiceMigrateState(
v int, is *terraform.InstanceState, meta interface{}) (*terraform.InstanceState, error) {
if is.Empty() {
log.Println("[DEBUG] Empty InstanceState; nothing to migrate.")
return is, nil
}

switch v {
case 0:
log.Println("[INFO] Found Compute Backend Service State v0; migrating to v1")
is, err := migrateBackendServiceStateV0toV1(is)
if err != nil {
return is, err
}
return is, nil
default:
return is, fmt.Errorf("Unexpected schema version: %d", v)
}
}

func migrateBackendServiceStateV0toV1(is *terraform.InstanceState) (*terraform.InstanceState, error) {
log.Printf("[DEBUG] Attributes before migration: %#v", is.Attributes)

oldHashToValue := map[string]map[string]interface{}{}
for k, v := range is.Attributes {
if !strings.HasPrefix(k, "backend.") || k == "backend.#" {
continue
}

// Key is now of the form backend.%d.%s
kParts := strings.Split(k, ".")

// Sanity check: two parts should be there and <N> should be a number
badFormat := false
if len(kParts) != 3 {
badFormat = true
} else if _, err := strconv.Atoi(kParts[1]); err != nil {
badFormat = true
}

if badFormat {
return is, fmt.Errorf("migration error: found backend key in unexpected format: %s", k)
}

if oldHashToValue[kParts[1]] == nil {
oldHashToValue[kParts[1]] = map[string]interface{}{}
}
oldHashToValue[kParts[1]][kParts[2]] = v
}

oldHashToNewHash := map[string]int{}
for k, v := range oldHashToValue {
oldHashToNewHash[k] = resourceGoogleComputeBackendServiceBackendHash(v)
}

values := map[string]string{}
for k, v := range is.Attributes {
if !strings.HasPrefix(k, "backend.") {
continue
}

if k == "backend.#" {
continue
}

// Key is now of the form backend.%d.%s
kParts := strings.Split(k, ".")
newKey := fmt.Sprintf("%s.%d.%s", kParts[0], oldHashToNewHash[kParts[1]], kParts[2])
values[newKey] = v
delete(is.Attributes, k)
}

for k, v := range values {
is.Attributes[k] = v
}

log.Printf("[DEBUG] Attributes after migration: %#v", is.Attributes)
return is, nil
}

<%- # the license inside this block applies to this file
# Copyright 2019 Google Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
-%>
func resourceGoogleComputeBackendServiceBackendHash(v interface{}) int {
if v == nil {
return 0
Expand Down

This file was deleted.

0 comments on commit 5011895

Please sign in to comment.