|
| 1 | +// Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved. |
| 2 | + |
| 3 | +package oci |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform/helper/schema" |
| 9 | + oci_object_storage "github.com/oracle/oci-go-sdk/objectstorage" |
| 10 | +) |
| 11 | + |
| 12 | +func init() { |
| 13 | + RegisterDatasource("oci_objectstorage_replication_policies", ObjectStorageReplicationPoliciesDataSource()) |
| 14 | +} |
| 15 | + |
| 16 | +func ObjectStorageReplicationPoliciesDataSource() *schema.Resource { |
| 17 | + return &schema.Resource{ |
| 18 | + Read: readObjectStorageReplicationPolicies, |
| 19 | + Schema: map[string]*schema.Schema{ |
| 20 | + "filter": dataSourceFiltersSchema(), |
| 21 | + "bucket": { |
| 22 | + Type: schema.TypeString, |
| 23 | + Required: true, |
| 24 | + }, |
| 25 | + "namespace": { |
| 26 | + Type: schema.TypeString, |
| 27 | + Required: true, |
| 28 | + }, |
| 29 | + "replication_policies": { |
| 30 | + Type: schema.TypeList, |
| 31 | + Computed: true, |
| 32 | + Elem: GetDataSourceItemSchema(ObjectStorageReplicationPolicyResource()), |
| 33 | + }, |
| 34 | + }, |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +func readObjectStorageReplicationPolicies(d *schema.ResourceData, m interface{}) error { |
| 39 | + sync := &ObjectStorageReplicationPoliciesDataSourceCrud{} |
| 40 | + sync.D = d |
| 41 | + sync.Client = m.(*OracleClients).objectStorageClient |
| 42 | + |
| 43 | + return ReadResource(sync) |
| 44 | +} |
| 45 | + |
| 46 | +type ObjectStorageReplicationPoliciesDataSourceCrud struct { |
| 47 | + D *schema.ResourceData |
| 48 | + Client *oci_object_storage.ObjectStorageClient |
| 49 | + Res *oci_object_storage.ListReplicationPoliciesResponse |
| 50 | +} |
| 51 | + |
| 52 | +func (s *ObjectStorageReplicationPoliciesDataSourceCrud) VoidState() { |
| 53 | + s.D.SetId("") |
| 54 | +} |
| 55 | + |
| 56 | +func (s *ObjectStorageReplicationPoliciesDataSourceCrud) Get() error { |
| 57 | + request := oci_object_storage.ListReplicationPoliciesRequest{} |
| 58 | + |
| 59 | + if bucket, ok := s.D.GetOkExists("bucket"); ok { |
| 60 | + tmp := bucket.(string) |
| 61 | + request.BucketName = &tmp |
| 62 | + } |
| 63 | + |
| 64 | + if namespace, ok := s.D.GetOkExists("namespace"); ok { |
| 65 | + tmp := namespace.(string) |
| 66 | + request.NamespaceName = &tmp |
| 67 | + } |
| 68 | + |
| 69 | + request.RequestMetadata.RetryPolicy = getRetryPolicy(false, "object_storage") |
| 70 | + |
| 71 | + response, err := s.Client.ListReplicationPolicies(context.Background(), request) |
| 72 | + if err != nil { |
| 73 | + return err |
| 74 | + } |
| 75 | + |
| 76 | + s.Res = &response |
| 77 | + request.Page = s.Res.OpcNextPage |
| 78 | + |
| 79 | + for request.Page != nil { |
| 80 | + listResponse, err := s.Client.ListReplicationPolicies(context.Background(), request) |
| 81 | + if err != nil { |
| 82 | + return err |
| 83 | + } |
| 84 | + |
| 85 | + s.Res.Items = append(s.Res.Items, listResponse.Items...) |
| 86 | + request.Page = listResponse.OpcNextPage |
| 87 | + } |
| 88 | + |
| 89 | + return nil |
| 90 | +} |
| 91 | + |
| 92 | +func (s *ObjectStorageReplicationPoliciesDataSourceCrud) SetData() error { |
| 93 | + if s.Res == nil { |
| 94 | + return nil |
| 95 | + } |
| 96 | + |
| 97 | + s.D.SetId(GenerateDataSourceID()) |
| 98 | + resources := []map[string]interface{}{} |
| 99 | + |
| 100 | + for _, r := range s.Res.Items { |
| 101 | + replicationPolicy := map[string]interface{}{} |
| 102 | + |
| 103 | + if r.DestinationBucketName != nil { |
| 104 | + replicationPolicy["destination_bucket_name"] = *r.DestinationBucketName |
| 105 | + } |
| 106 | + |
| 107 | + if r.DestinationRegionName != nil { |
| 108 | + replicationPolicy["destination_region_name"] = *r.DestinationRegionName |
| 109 | + } |
| 110 | + |
| 111 | + if r.Id != nil { |
| 112 | + replicationPolicy["id"] = *r.Id |
| 113 | + } |
| 114 | + |
| 115 | + if r.Name != nil { |
| 116 | + replicationPolicy["name"] = *r.Name |
| 117 | + } |
| 118 | + |
| 119 | + replicationPolicy["status"] = r.Status |
| 120 | + |
| 121 | + if r.StatusMessage != nil { |
| 122 | + replicationPolicy["status_message"] = *r.StatusMessage |
| 123 | + } |
| 124 | + |
| 125 | + if r.TimeCreated != nil { |
| 126 | + replicationPolicy["time_created"] = r.TimeCreated.String() |
| 127 | + } |
| 128 | + |
| 129 | + if r.TimeLastSync != nil { |
| 130 | + replicationPolicy["time_last_sync"] = r.TimeLastSync.String() |
| 131 | + } |
| 132 | + |
| 133 | + resources = append(resources, replicationPolicy) |
| 134 | + } |
| 135 | + |
| 136 | + if f, fOk := s.D.GetOkExists("filter"); fOk { |
| 137 | + resources = ApplyFilters(f.(*schema.Set), resources, ObjectStorageReplicationPoliciesDataSource().Schema["replication_policies"].Elem.(*schema.Resource).Schema) |
| 138 | + } |
| 139 | + |
| 140 | + if err := s.D.Set("replication_policies", resources); err != nil { |
| 141 | + return err |
| 142 | + } |
| 143 | + |
| 144 | + return nil |
| 145 | +} |
0 commit comments