|
| 1 | +// Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. |
| 2 | + |
| 3 | +package provider |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "strconv" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform/helper/schema" |
| 10 | + oci_identity "github.com/oracle/oci-go-sdk/identity" |
| 11 | +) |
| 12 | + |
| 13 | +func CompartmentDataSource() *schema.Resource { |
| 14 | + return &schema.Resource{ |
| 15 | + Read: readSingularCompartment, |
| 16 | + Schema: map[string]*schema.Schema{ |
| 17 | + "id": { |
| 18 | + Type: schema.TypeString, |
| 19 | + Required: true, |
| 20 | + }, |
| 21 | + "compartment_id": { |
| 22 | + Type: schema.TypeString, |
| 23 | + Computed: true, |
| 24 | + }, |
| 25 | + // Computed |
| 26 | + "defined_tags": { |
| 27 | + Type: schema.TypeMap, |
| 28 | + Computed: true, |
| 29 | + Elem: schema.TypeString, |
| 30 | + }, |
| 31 | + "description": { |
| 32 | + Type: schema.TypeString, |
| 33 | + Computed: true, |
| 34 | + }, |
| 35 | + "freeform_tags": { |
| 36 | + Type: schema.TypeMap, |
| 37 | + Computed: true, |
| 38 | + Elem: schema.TypeString, |
| 39 | + }, |
| 40 | + "inactive_state": { |
| 41 | + Type: schema.TypeString, |
| 42 | + Computed: true, |
| 43 | + }, |
| 44 | + "is_accessible": { |
| 45 | + Type: schema.TypeBool, |
| 46 | + Computed: true, |
| 47 | + }, |
| 48 | + "name": { |
| 49 | + Type: schema.TypeString, |
| 50 | + Computed: true, |
| 51 | + }, |
| 52 | + "state": { |
| 53 | + Type: schema.TypeString, |
| 54 | + Computed: true, |
| 55 | + }, |
| 56 | + "time_created": { |
| 57 | + Type: schema.TypeString, |
| 58 | + Computed: true, |
| 59 | + }, |
| 60 | + }, |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +func readSingularCompartment(d *schema.ResourceData, m interface{}) error { |
| 65 | + sync := &CompartmentDataSourceCrud{} |
| 66 | + sync.D = d |
| 67 | + sync.Client = m.(*OracleClients).identityClient |
| 68 | + |
| 69 | + return ReadResource(sync) |
| 70 | +} |
| 71 | + |
| 72 | +type CompartmentDataSourceCrud struct { |
| 73 | + D *schema.ResourceData |
| 74 | + Client *oci_identity.IdentityClient |
| 75 | + Res *oci_identity.GetCompartmentResponse |
| 76 | +} |
| 77 | + |
| 78 | +func (s *CompartmentDataSourceCrud) VoidState() { |
| 79 | + s.D.SetId("") |
| 80 | +} |
| 81 | + |
| 82 | +func (s *CompartmentDataSourceCrud) Get() error { |
| 83 | + request := oci_identity.GetCompartmentRequest{} |
| 84 | + |
| 85 | + // API ambiguity: the GET identifier for a single compartment is "compartmentId", but on the returned object |
| 86 | + // it's "id", and "compartmentId" holds the _parent_ compartment id. |
| 87 | + if id, ok := s.D.GetOkExists("id"); ok { |
| 88 | + tmp := id.(string) |
| 89 | + request.CompartmentId = &tmp |
| 90 | + } |
| 91 | + |
| 92 | + request.RequestMetadata.RetryPolicy = getRetryPolicy(false, "identity") |
| 93 | + |
| 94 | + response, err := s.Client.GetCompartment(context.Background(), request) |
| 95 | + if err != nil { |
| 96 | + return err |
| 97 | + } |
| 98 | + |
| 99 | + s.Res = &response |
| 100 | + return nil |
| 101 | +} |
| 102 | + |
| 103 | +func (s *CompartmentDataSourceCrud) SetData() error { |
| 104 | + if s.Res == nil { |
| 105 | + return nil |
| 106 | + } |
| 107 | + |
| 108 | + s.D.SetId(*s.Res.Id) |
| 109 | + |
| 110 | + if s.Res.DefinedTags != nil { |
| 111 | + s.D.Set("defined_tags", definedTagsToMap(s.Res.DefinedTags)) |
| 112 | + } |
| 113 | + |
| 114 | + if s.Res.CompartmentId != nil { |
| 115 | + s.D.Set("compartment_id", *s.Res.CompartmentId) |
| 116 | + } |
| 117 | + |
| 118 | + if s.Res.Description != nil { |
| 119 | + s.D.Set("description", *s.Res.Description) |
| 120 | + } |
| 121 | + |
| 122 | + s.D.Set("freeform_tags", s.Res.FreeformTags) |
| 123 | + |
| 124 | + if s.Res.InactiveStatus != nil { |
| 125 | + s.D.Set("inactive_state", strconv.FormatInt(*s.Res.InactiveStatus, 10)) |
| 126 | + } |
| 127 | + |
| 128 | + if s.Res.IsAccessible != nil { |
| 129 | + s.D.Set("is_accessible", *s.Res.IsAccessible) |
| 130 | + } |
| 131 | + |
| 132 | + if s.Res.Name != nil { |
| 133 | + s.D.Set("name", *s.Res.Name) |
| 134 | + } |
| 135 | + |
| 136 | + s.D.Set("state", s.Res.LifecycleState) |
| 137 | + |
| 138 | + if s.Res.TimeCreated != nil { |
| 139 | + s.D.Set("time_created", s.Res.TimeCreated.String()) |
| 140 | + } |
| 141 | + |
| 142 | + return nil |
| 143 | +} |
0 commit comments