|
| 1 | +// Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. |
| 2 | + |
| 3 | +package provider |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform/helper/schema" |
| 9 | + oci_identity "github.com/oracle/oci-go-sdk/identity" |
| 10 | + |
| 11 | + "github.com/oracle/terraform-provider-oci/crud" |
| 12 | +) |
| 13 | + |
| 14 | +func RegionSubscriptionsDataSource() *schema.Resource { |
| 15 | + return &schema.Resource{ |
| 16 | + Read: readRegionSubscriptions, |
| 17 | + Schema: map[string]*schema.Schema{ |
| 18 | + "filter": dataSourceFiltersSchema(), |
| 19 | + "tenancy_id": { |
| 20 | + Type: schema.TypeString, |
| 21 | + Required: true, |
| 22 | + }, |
| 23 | + "region_subscriptions": { |
| 24 | + Type: schema.TypeList, |
| 25 | + Computed: true, |
| 26 | + Elem: &schema.Resource{ |
| 27 | + Schema: map[string]*schema.Schema{ |
| 28 | + // Required |
| 29 | + "region_key": { |
| 30 | + Type: schema.TypeString, |
| 31 | + Required: true, |
| 32 | + ForceNew: true, |
| 33 | + }, |
| 34 | + "tenancy_id": { |
| 35 | + Type: schema.TypeString, |
| 36 | + Required: true, |
| 37 | + ForceNew: true, |
| 38 | + }, |
| 39 | + |
| 40 | + // Optional |
| 41 | + |
| 42 | + // Computed |
| 43 | + "is_home_region": { |
| 44 | + Type: schema.TypeBool, |
| 45 | + Computed: true, |
| 46 | + }, |
| 47 | + "region_name": { |
| 48 | + Type: schema.TypeString, |
| 49 | + Computed: true, |
| 50 | + }, |
| 51 | + "state": { |
| 52 | + Type: schema.TypeString, |
| 53 | + Computed: true, |
| 54 | + }, |
| 55 | + }, |
| 56 | + }, |
| 57 | + }, |
| 58 | + }, |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func readRegionSubscriptions(d *schema.ResourceData, m interface{}) error { |
| 63 | + sync := &RegionSubscriptionsDataSourceCrud{} |
| 64 | + sync.D = d |
| 65 | + sync.Client = m.(*OracleClients).identityClient |
| 66 | + |
| 67 | + return crud.ReadResource(sync) |
| 68 | +} |
| 69 | + |
| 70 | +type RegionSubscriptionsDataSourceCrud struct { |
| 71 | + D *schema.ResourceData |
| 72 | + Client *oci_identity.IdentityClient |
| 73 | + Res *oci_identity.ListRegionSubscriptionsResponse |
| 74 | +} |
| 75 | + |
| 76 | +func (s *RegionSubscriptionsDataSourceCrud) VoidState() { |
| 77 | + s.D.SetId("") |
| 78 | +} |
| 79 | + |
| 80 | +func (s *RegionSubscriptionsDataSourceCrud) Get() error { |
| 81 | + request := oci_identity.ListRegionSubscriptionsRequest{} |
| 82 | + |
| 83 | + if tenancyId, ok := s.D.GetOkExists("tenancy_id"); ok { |
| 84 | + tmp := tenancyId.(string) |
| 85 | + request.TenancyId = &tmp |
| 86 | + } |
| 87 | + |
| 88 | + request.RequestMetadata.RetryPolicy = getRetryPolicy(false, "identity") |
| 89 | + |
| 90 | + response, err := s.Client.ListRegionSubscriptions(context.Background(), request) |
| 91 | + if err != nil { |
| 92 | + return err |
| 93 | + } |
| 94 | + |
| 95 | + s.Res = &response |
| 96 | + return nil |
| 97 | +} |
| 98 | + |
| 99 | +func (s *RegionSubscriptionsDataSourceCrud) SetData() { |
| 100 | + if s.Res == nil { |
| 101 | + return |
| 102 | + } |
| 103 | + |
| 104 | + s.D.SetId(crud.GenerateDataSourceID()) |
| 105 | + resources := []map[string]interface{}{} |
| 106 | + |
| 107 | + for _, r := range s.Res.Items { |
| 108 | + regionSubscription := map[string]interface{}{} |
| 109 | + |
| 110 | + if r.IsHomeRegion != nil { |
| 111 | + regionSubscription["is_home_region"] = *r.IsHomeRegion |
| 112 | + } |
| 113 | + |
| 114 | + if r.RegionKey != nil { |
| 115 | + regionSubscription["region_key"] = *r.RegionKey |
| 116 | + } |
| 117 | + |
| 118 | + if r.RegionName != nil { |
| 119 | + regionSubscription["region_name"] = *r.RegionName |
| 120 | + } |
| 121 | + |
| 122 | + regionSubscription["state"] = r.Status |
| 123 | + |
| 124 | + resources = append(resources, regionSubscription) |
| 125 | + } |
| 126 | + |
| 127 | + if f, fOk := s.D.GetOkExists("filter"); fOk { |
| 128 | + resources = ApplyFilters(f.(*schema.Set), resources) |
| 129 | + } |
| 130 | + |
| 131 | + if err := s.D.Set("region_subscriptions", resources); err != nil { |
| 132 | + panic(err) |
| 133 | + } |
| 134 | + |
| 135 | + |
| 136 | + return |
| 137 | +} |
0 commit comments