本文整理匯總了Golang中github.com/hashicorp/terraform/terraform.InstanceState.ID方法的典型用法代碼示例。如果您正苦於以下問題:Golang InstanceState.ID方法的具體用法?Golang InstanceState.ID怎麽用?Golang InstanceState.ID使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/hashicorp/terraform/terraform.InstanceState
的用法示例。
在下文中一共展示了InstanceState.ID方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: State
// State returns the new InstanceState after the diff and any Set
// calls.
func (d *ResourceData) State() *terraform.InstanceState {
var result terraform.InstanceState
result.ID = d.Id()
// If we have no ID, then this resource doesn't exist and we just
// return nil.
if result.ID == "" {
return nil
}
// In order to build the final state attributes, we read the full
// attribute set as a map[string]interface{}, write it to a MapFieldWriter,
// and then use that map.
rawMap := make(map[string]interface{})
for k := range d.schema {
source := getSourceSet
if d.partial {
source = getSourceState
if _, ok := d.partialMap[k]; ok {
source = getSourceSet
}
}
raw := d.get([]string{k}, source)
if raw.Exists && !raw.Computed {
rawMap[k] = raw.Value
if raw.ValueProcessed != nil {
rawMap[k] = raw.ValueProcessed
}
}
}
mapW := &MapFieldWriter{Schema: d.schema}
if err := mapW.WriteField(nil, rawMap); err != nil {
return nil
}
result.Attributes = mapW.Map()
result.Ephemeral.ConnInfo = d.ConnInfo()
// TODO: This is hacky and we can remove this when we have a proper
// state writer. We should instead have a proper StateFieldWriter
// and use that.
for k, schema := range d.schema {
if schema.Type != TypeMap {
continue
}
if result.Attributes[k] == "" {
delete(result.Attributes, k)
}
}
if v := d.Id(); v != "" {
result.Attributes["id"] = d.Id()
}
return &result
}
示例2: Apply
// Apply performs a create or update depending on the diff, and calls
// the proper function on the matching Resource.
func (m *Map) Apply(
info *terraform.InstanceInfo,
s *terraform.InstanceState,
d *terraform.InstanceDiff,
meta interface{}) (*terraform.InstanceState, error) {
r, ok := m.Mapping[info.Type]
if !ok {
return nil, fmt.Errorf("Unknown resource type: %s", info.Type)
}
if d.Destroy || d.RequiresNew() {
if s.ID != "" {
// Destroy the resource if it is created
err := r.Destroy(s, meta)
if err != nil {
return s, err
}
s.ID = ""
}
// If we're only destroying, and not creating, then return now.
// Otherwise, we continue so that we can create a new resource.
if !d.RequiresNew() {
return nil, nil
}
}
var result *terraform.InstanceState
var err error
if s.ID == "" {
result, err = r.Create(s, d, meta)
} else {
if r.Update == nil {
return s, fmt.Errorf(
"Resource type '%s' doesn't support update",
info.Type)
}
result, err = r.Update(s, d, meta)
}
if result != nil {
if result.Attributes == nil {
result.Attributes = make(map[string]string)
}
result.Attributes["id"] = result.ID
}
return result, err
}
示例3: migrateSGRuleStateV0toV1
func migrateSGRuleStateV0toV1(is *terraform.InstanceState) (*terraform.InstanceState, error) {
if is.Empty() {
log.Println("[DEBUG] Empty InstanceState; nothing to migrate.")
return is, nil
}
perm, err := migrateExpandIPPerm(is.Attributes)
if err != nil {
return nil, fmt.Errorf("[WARN] Error making new IP Permission in Security Group migration")
}
log.Printf("[DEBUG] Attributes before migration: %#v", is.Attributes)
newID := ipPermissionIDHash(is.Attributes["security_group_id"], is.Attributes["type"], perm)
is.Attributes["id"] = newID
is.ID = newID
log.Printf("[DEBUG] Attributes after migration: %#v, new id: %s", is.Attributes, newID)
return is, nil
}
示例4: migrateCloudFlareRecordStateV0toV1
func migrateCloudFlareRecordStateV0toV1(is *terraform.InstanceState, meta interface{}) (*terraform.InstanceState, error) {
if is.Empty() {
log.Println("[DEBUG] Empty InstanceState; nothing to migrate.")
return is, nil
}
log.Printf("[DEBUG] Attributes before migration: %#v", is.Attributes)
client := meta.(*cloudflare.API)
// look up new id based on attributes
domain := is.Attributes["domain"]
zoneId, err := client.ZoneIDByName(domain)
if err != nil {
return is, fmt.Errorf("Error finding zone %q: %s", domain, err)
}
// all other information is ignored in the DNSRecords call
searchRecord := cloudflare.DNSRecord{
Type: is.Attributes["type"],
Name: is.Attributes["hostname"],
Content: is.Attributes["value"],
}
records, err := client.DNSRecords(zoneId, searchRecord)
if err != nil {
return is, err
}
for _, r := range records {
if is.Attributes["ttl"] != "" {
v, err := strconv.Atoi(is.Attributes["ttl"])
if err != nil {
return is, fmt.Errorf("Error converting ttl to int in CloudFlare Record Migration")
}
if v != r.TTL {
continue
}
}
if is.Attributes["proxied"] != "" {
b, err := strconv.ParseBool(is.Attributes["proxied"])
if err != nil {
return is, fmt.Errorf("Error converting proxied to bool in CloudFlare Record Migration")
}
if b != r.Proxied {
continue
}
}
if is.Attributes["priority"] != "" {
v, err := strconv.Atoi(is.Attributes["priority"])
if err != nil {
return is, fmt.Errorf("Error converting priority to int in CloudFlare Record Migration")
}
if v != r.Priority {
continue
}
}
// assume record found
is.Attributes["id"] = r.ID
is.ID = r.ID
log.Printf("[DEBUG] Attributes after migration: %#v", is.Attributes)
return is, nil
}
// assume no record found
log.Printf("[DEBUG] Attributes after no migration: %#v", is.Attributes)
return is, fmt.Errorf("No matching Record found")
}
示例5: State
// State returns the new InstanceState after the diff and any Set
// calls.
func (d *ResourceData) State() *terraform.InstanceState {
var result terraform.InstanceState
result.ID = d.Id()
result.Meta = d.meta
// If we have no ID, then this resource doesn't exist and we just
// return nil.
if result.ID == "" {
return nil
}
// Look for a magic key in the schema that determines we skip the
// integrity check of fields existing in the schema, allowing dynamic
// keys to be created.
hasDynamicAttributes := false
for k, _ := range d.schema {
if k == "__has_dynamic_attributes" {
hasDynamicAttributes = true
log.Printf("[INFO] Resource %s has dynamic attributes", result.ID)
}
}
// In order to build the final state attributes, we read the full
// attribute set as a map[string]interface{}, write it to a MapFieldWriter,
// and then use that map.
rawMap := make(map[string]interface{})
for k := range d.schema {
source := getSourceSet
if d.partial {
source = getSourceState
if _, ok := d.partialMap[k]; ok {
source = getSourceSet
}
}
raw := d.get([]string{k}, source)
if raw.Exists && !raw.Computed {
rawMap[k] = raw.Value
if raw.ValueProcessed != nil {
rawMap[k] = raw.ValueProcessed
}
}
}
mapW := &MapFieldWriter{Schema: d.schema}
if err := mapW.WriteField(nil, rawMap); err != nil {
return nil
}
result.Attributes = mapW.Map()
if hasDynamicAttributes {
// If we have dynamic attributes, just copy the attributes map
// one for one into the result attributes.
for k, v := range d.setWriter.Map() {
// Don't clobber schema values. This limits usage of dynamic
// attributes to names which _do not_ conflict with schema
// keys!
if _, ok := result.Attributes[k]; !ok {
result.Attributes[k] = v
}
}
}
if d.newState != nil {
result.Ephemeral = d.newState.Ephemeral
}
// TODO: This is hacky and we can remove this when we have a proper
// state writer. We should instead have a proper StateFieldWriter
// and use that.
for k, schema := range d.schema {
if schema.Type != TypeMap {
continue
}
if result.Attributes[k] == "" {
delete(result.Attributes, k)
}
}
if v := d.Id(); v != "" {
result.Attributes["id"] = d.Id()
}
if d.state != nil {
result.Tainted = d.state.Tainted
}
return &result
}