|
| 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 RegionsDataSource() *schema.Resource { |
| 15 | + return &schema.Resource{ |
| 16 | + Read: readRegions, |
| 17 | + Schema: map[string]*schema.Schema{ |
| 18 | + "filter": dataSourceFiltersSchema(), |
| 19 | + "regions": { |
| 20 | + Type: schema.TypeList, |
| 21 | + Computed: true, |
| 22 | + Elem: &schema.Resource{ |
| 23 | + Schema: map[string]*schema.Schema{ |
| 24 | + // Required |
| 25 | + |
| 26 | + // Optional |
| 27 | + |
| 28 | + // Computed |
| 29 | + "key": { |
| 30 | + Type: schema.TypeString, |
| 31 | + Computed: true, |
| 32 | + }, |
| 33 | + "name": { |
| 34 | + Type: schema.TypeString, |
| 35 | + Computed: true, |
| 36 | + }, |
| 37 | + }, |
| 38 | + }, |
| 39 | + }, |
| 40 | + }, |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +func readRegions(d *schema.ResourceData, m interface{}) error { |
| 45 | + sync := &RegionsDataSourceCrud{} |
| 46 | + sync.D = d |
| 47 | + sync.Client = m.(*OracleClients).identityClient |
| 48 | + |
| 49 | + return crud.ReadResource(sync) |
| 50 | +} |
| 51 | + |
| 52 | +type RegionsDataSourceCrud struct { |
| 53 | + D *schema.ResourceData |
| 54 | + Client *oci_identity.IdentityClient |
| 55 | + Res *oci_identity.ListRegionsResponse |
| 56 | +} |
| 57 | + |
| 58 | +func (s *RegionsDataSourceCrud) VoidState() { |
| 59 | + s.D.SetId("") |
| 60 | +} |
| 61 | + |
| 62 | +func (s *RegionsDataSourceCrud) Get() error { |
| 63 | + request := oci_identity.ListRegionsRequest{} |
| 64 | + |
| 65 | + request.RequestMetadata.RetryPolicy = getRetryPolicy(false, "identity") |
| 66 | + |
| 67 | + response, err := s.Client.ListRegions(context.Background()) |
| 68 | + if err != nil { |
| 69 | + return err |
| 70 | + } |
| 71 | + |
| 72 | + s.Res = &response |
| 73 | + return nil |
| 74 | +} |
| 75 | + |
| 76 | +func (s *RegionsDataSourceCrud) SetData() { |
| 77 | + if s.Res == nil { |
| 78 | + return |
| 79 | + } |
| 80 | + |
| 81 | + s.D.SetId(crud.GenerateDataSourceID()) |
| 82 | + resources := []map[string]interface{}{} |
| 83 | + |
| 84 | + for _, r := range s.Res.Items { |
| 85 | + region := map[string]interface{}{} |
| 86 | + |
| 87 | + if r.Key != nil { |
| 88 | + region["key"] = *r.Key |
| 89 | + } |
| 90 | + |
| 91 | + if r.Name != nil { |
| 92 | + region["name"] = *r.Name |
| 93 | + } |
| 94 | + |
| 95 | + resources = append(resources, region) |
| 96 | + } |
| 97 | + |
| 98 | + if f, fOk := s.D.GetOkExists("filter"); fOk { |
| 99 | + resources = ApplyFilters(f.(*schema.Set), resources) |
| 100 | + } |
| 101 | + |
| 102 | + if err := s.D.Set("regions", resources); err != nil { |
| 103 | + panic(err) |
| 104 | + } |
| 105 | + |
| 106 | + return |
| 107 | +} |
0 commit comments