|
| 1 | +// Copyright (c) 2017, 2021, Oracle and/or its affiliates. All rights reserved. |
| 2 | +// Licensed under the Mozilla Public License v2.0 |
| 3 | + |
| 4 | +package oci |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 10 | + oci_metering_computation "github.com/oracle/oci-go-sdk/v45/usageapi" |
| 11 | +) |
| 12 | + |
| 13 | +func init() { |
| 14 | + RegisterDatasource("oci_metering_computation_custom_table", MeteringComputationCustomTableDataSource()) |
| 15 | +} |
| 16 | + |
| 17 | +func MeteringComputationCustomTableDataSource() *schema.Resource { |
| 18 | + fieldMap := make(map[string]*schema.Schema) |
| 19 | + fieldMap["custom_table_id"] = &schema.Schema{ |
| 20 | + Type: schema.TypeString, |
| 21 | + Required: true, |
| 22 | + } |
| 23 | + return GetSingularDataSourceItemSchema(MeteringComputationCustomTableResource(), fieldMap, readSingularMeteringComputationCustomTable) |
| 24 | +} |
| 25 | + |
| 26 | +func readSingularMeteringComputationCustomTable(d *schema.ResourceData, m interface{}) error { |
| 27 | + sync := &MeteringComputationCustomTableDataSourceCrud{} |
| 28 | + sync.D = d |
| 29 | + sync.Client = m.(*OracleClients).usageapiClient() |
| 30 | + |
| 31 | + return ReadResource(sync) |
| 32 | +} |
| 33 | + |
| 34 | +type MeteringComputationCustomTableDataSourceCrud struct { |
| 35 | + D *schema.ResourceData |
| 36 | + Client *oci_metering_computation.UsageapiClient |
| 37 | + Res *oci_metering_computation.GetCustomTableResponse |
| 38 | +} |
| 39 | + |
| 40 | +func (s *MeteringComputationCustomTableDataSourceCrud) VoidState() { |
| 41 | + s.D.SetId("") |
| 42 | +} |
| 43 | + |
| 44 | +func (s *MeteringComputationCustomTableDataSourceCrud) Get() error { |
| 45 | + request := oci_metering_computation.GetCustomTableRequest{} |
| 46 | + |
| 47 | + if customTableId, ok := s.D.GetOkExists("custom_table_id"); ok { |
| 48 | + tmp := customTableId.(string) |
| 49 | + request.CustomTableId = &tmp |
| 50 | + } |
| 51 | + |
| 52 | + request.RequestMetadata.RetryPolicy = getRetryPolicy(false, "metering_computation") |
| 53 | + |
| 54 | + response, err := s.Client.GetCustomTable(context.Background(), request) |
| 55 | + if err != nil { |
| 56 | + return err |
| 57 | + } |
| 58 | + |
| 59 | + s.Res = &response |
| 60 | + return nil |
| 61 | +} |
| 62 | + |
| 63 | +func (s *MeteringComputationCustomTableDataSourceCrud) SetData() error { |
| 64 | + if s.Res == nil { |
| 65 | + return nil |
| 66 | + } |
| 67 | + |
| 68 | + s.D.SetId(*s.Res.Id) |
| 69 | + |
| 70 | + if s.Res.CompartmentId != nil { |
| 71 | + s.D.Set("compartment_id", *s.Res.CompartmentId) |
| 72 | + } |
| 73 | + |
| 74 | + if s.Res.SavedCustomTable != nil { |
| 75 | + s.D.Set("saved_custom_table", []interface{}{SavedCustomTableToMap(s.Res.SavedCustomTable)}) |
| 76 | + } else { |
| 77 | + s.D.Set("saved_custom_table", nil) |
| 78 | + } |
| 79 | + |
| 80 | + if s.Res.SavedReportId != nil { |
| 81 | + s.D.Set("saved_report_id", *s.Res.SavedReportId) |
| 82 | + } |
| 83 | + |
| 84 | + return nil |
| 85 | +} |
0 commit comments