Move method out of resource file to prepare for autogen (#1102)

This commit is contained in:
Vincent Roseberry 2018-02-20 12:40:02 -08:00 committed by GitHub
parent 297b0a80e0
commit 304c0aa6cb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 107 additions and 108 deletions

View File

@ -158,3 +158,14 @@ func flattenInstancesWithNamedPorts(insts []*compute.InstanceWithNamedPorts) []m
}
return result
}
func flattenNamedPorts(namedPorts []*compute.NamedPort) []map[string]interface{} {
result := make([]map[string]interface{}, 0, len(namedPorts))
for _, namedPort := range namedPorts {
namedPortMap := make(map[string]interface{})
namedPortMap["name"] = namedPort.Name
namedPortMap["port"] = namedPort.Port
result = append(result, namedPortMap)
}
return result
}

View File

@ -4,7 +4,6 @@ import (
"fmt"
"regexp"
"strings"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
@ -251,63 +250,3 @@ func resourceComputeAddressImportState(d *schema.ResourceData, meta interface{})
return []*schema.ResourceData{d}, nil
}
type computeAddressId struct {
Project string
Region string
Name string
}
func (s computeAddressId) canonicalId() string {
return fmt.Sprintf(computeAddressIdTemplate, s.Project, s.Region, s.Name)
}
func parseComputeAddressId(id string, config *Config) (*computeAddressId, error) {
var parts []string
if computeAddressLinkRegex.MatchString(id) {
parts = computeAddressLinkRegex.FindStringSubmatch(id)
return &computeAddressId{
Project: parts[1],
Region: parts[2],
Name: parts[3],
}, nil
} else {
parts = strings.Split(id, "/")
}
if len(parts) == 3 {
return &computeAddressId{
Project: parts[0],
Region: parts[1],
Name: parts[2],
}, nil
} else if len(parts) == 2 {
// Project is optional.
if config.Project == "" {
return nil, fmt.Errorf("The default project for the provider must be set when using the `{region}/{name}` id format.")
}
return &computeAddressId{
Project: config.Project,
Region: parts[0],
Name: parts[1],
}, nil
} else if len(parts) == 1 {
// Project and region is optional
if config.Project == "" {
return nil, fmt.Errorf("The default project for the provider must be set when using the `{name}` id format.")
}
if config.Region == "" {
return nil, fmt.Errorf("The default region for the provider must be set when using the `{name}` id format.")
}
return &computeAddressId{
Project: config.Project,
Region: config.Region,
Name: parts[0],
}, nil
}
return nil, fmt.Errorf("Invalid compute address id. Expecting resource link, `{project}/{region}/{name}`, `{region}/{name}` or `{name}` format.")
}

View File

@ -4,6 +4,7 @@ import (
"fmt"
"github.com/hashicorp/terraform/terraform"
"log"
"strings"
)
func resourceComputeAddressMigrateState(v int, is *terraform.InstanceState, meta interface{}) (*terraform.InstanceState, error) {
@ -46,3 +47,63 @@ func migrateComputeAddressV0toV1(is *terraform.InstanceState, meta interface{})
log.Printf("[DEBUG] ID after migration: %s", is.ID)
return is, nil
}
type computeAddressId struct {
Project string
Region string
Name string
}
func (s computeAddressId) canonicalId() string {
return fmt.Sprintf(computeAddressIdTemplate, s.Project, s.Region, s.Name)
}
func parseComputeAddressId(id string, config *Config) (*computeAddressId, error) {
var parts []string
if computeAddressLinkRegex.MatchString(id) {
parts = computeAddressLinkRegex.FindStringSubmatch(id)
return &computeAddressId{
Project: parts[1],
Region: parts[2],
Name: parts[3],
}, nil
} else {
parts = strings.Split(id, "/")
}
if len(parts) == 3 {
return &computeAddressId{
Project: parts[0],
Region: parts[1],
Name: parts[2],
}, nil
} else if len(parts) == 2 {
// Project is optional.
if config.Project == "" {
return nil, fmt.Errorf("The default project for the provider must be set when using the `{region}/{name}` id format.")
}
return &computeAddressId{
Project: config.Project,
Region: parts[0],
Name: parts[1],
}, nil
} else if len(parts) == 1 {
// Project and region is optional
if config.Project == "" {
return nil, fmt.Errorf("The default project for the provider must be set when using the `{name}` id format.")
}
if config.Region == "" {
return nil, fmt.Errorf("The default region for the provider must be set when using the `{name}` id format.")
}
return &computeAddressId{
Project: config.Project,
Region: config.Region,
Name: parts[0],
}, nil
}
return nil, fmt.Errorf("Invalid compute address id. Expecting resource link, `{project}/{region}/{name}`, `{region}/{name}` or `{name}` format.")
}

View File

@ -1,13 +1,11 @@
package google
import (
"bytes"
"crypto/sha256"
"errors"
"fmt"
"log"
"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/schema"
"google.golang.org/api/compute/v1"
)
@ -458,36 +456,3 @@ func expandBackendService(d *schema.ResourceData) (*compute.BackendService, erro
return service, nil
}
func resourceGoogleComputeBackendServiceBackendHash(v interface{}) int {
if v == nil {
return 0
}
var buf bytes.Buffer
m := v.(map[string]interface{})
group, _ := getRelativePath(m["group"].(string))
buf.WriteString(fmt.Sprintf("%s-", group))
if v, ok := m["balancing_mode"]; ok {
buf.WriteString(fmt.Sprintf("%s-", v.(string)))
}
if v, ok := m["capacity_scaler"]; ok {
buf.WriteString(fmt.Sprintf("%f-", v.(float64)))
}
if v, ok := m["description"]; ok {
buf.WriteString(fmt.Sprintf("%s-", v.(string)))
}
if v, ok := m["max_rate"]; ok {
buf.WriteString(fmt.Sprintf("%d-", int64(v.(int))))
}
if v, ok := m["max_rate_per_instance"]; ok {
buf.WriteString(fmt.Sprintf("%f-", v.(float64)))
}
if v, ok := m["max_rate_per_instance"]; ok {
buf.WriteString(fmt.Sprintf("%f-", v.(float64)))
}
return hashcode.String(buf.String())
}

View File

@ -6,6 +6,8 @@ import (
"strconv"
"strings"
"bytes"
"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/terraform"
)
@ -88,3 +90,36 @@ func migrateBackendServiceStateV0toV1(is *terraform.InstanceState) (*terraform.I
log.Printf("[DEBUG] Attributes after migration: %#v", is.Attributes)
return is, nil
}
func resourceGoogleComputeBackendServiceBackendHash(v interface{}) int {
if v == nil {
return 0
}
var buf bytes.Buffer
m := v.(map[string]interface{})
group, _ := getRelativePath(m["group"].(string))
buf.WriteString(fmt.Sprintf("%s-", group))
if v, ok := m["balancing_mode"]; ok {
buf.WriteString(fmt.Sprintf("%s-", v.(string)))
}
if v, ok := m["capacity_scaler"]; ok {
buf.WriteString(fmt.Sprintf("%f-", v.(float64)))
}
if v, ok := m["description"]; ok {
buf.WriteString(fmt.Sprintf("%s-", v.(string)))
}
if v, ok := m["max_rate"]; ok {
buf.WriteString(fmt.Sprintf("%d-", int64(v.(int))))
}
if v, ok := m["max_rate_per_instance"]; ok {
buf.WriteString(fmt.Sprintf("%f-", v.(float64)))
}
if v, ok := m["max_rate_per_instance"]; ok {
buf.WriteString(fmt.Sprintf("%f-", v.(float64)))
}
return hashcode.String(buf.String())
}

View File

@ -388,15 +388,3 @@ func resourceComputeInstanceGroupImportState(d *schema.ResourceData, meta interf
return []*schema.ResourceData{d}, nil
}
func flattenNamedPorts(namedPorts []*compute.NamedPort) []map[string]interface{} {
result := make([]map[string]interface{}, 0, len(namedPorts))
for _, namedPort := range namedPorts {
namedPortMap := make(map[string]interface{})
namedPortMap["name"] = namedPort.Name
namedPortMap["port"] = namedPort.Port
result = append(result, namedPortMap)
}
return result
}