|
| 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 opa |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 10 | + oci_opa "github.com/oracle/oci-go-sdk/v65/opa" |
| 11 | + |
| 12 | + "github.com/oracle/terraform-provider-oci/internal/client" |
| 13 | + "github.com/oracle/terraform-provider-oci/internal/tfresource" |
| 14 | +) |
| 15 | + |
| 16 | +func OpaOpaInstanceDataSource() *schema.Resource { |
| 17 | + fieldMap := make(map[string]*schema.Schema) |
| 18 | + fieldMap["opa_instance_id"] = &schema.Schema{ |
| 19 | + Type: schema.TypeString, |
| 20 | + Required: true, |
| 21 | + } |
| 22 | + return tfresource.GetSingularDataSourceItemSchema(OpaOpaInstanceResource(), fieldMap, readSingularOpaOpaInstance) |
| 23 | +} |
| 24 | + |
| 25 | +func readSingularOpaOpaInstance(d *schema.ResourceData, m interface{}) error { |
| 26 | + sync := &OpaOpaInstanceDataSourceCrud{} |
| 27 | + sync.D = d |
| 28 | + sync.Client = m.(*client.OracleClients).OpaInstanceClient() |
| 29 | + |
| 30 | + return tfresource.ReadResource(sync) |
| 31 | +} |
| 32 | + |
| 33 | +type OpaOpaInstanceDataSourceCrud struct { |
| 34 | + D *schema.ResourceData |
| 35 | + Client *oci_opa.OpaInstanceClient |
| 36 | + Res *oci_opa.GetOpaInstanceResponse |
| 37 | +} |
| 38 | + |
| 39 | +func (s *OpaOpaInstanceDataSourceCrud) VoidState() { |
| 40 | + s.D.SetId("") |
| 41 | +} |
| 42 | + |
| 43 | +func (s *OpaOpaInstanceDataSourceCrud) Get() error { |
| 44 | + request := oci_opa.GetOpaInstanceRequest{} |
| 45 | + |
| 46 | + if opaInstanceId, ok := s.D.GetOkExists("opa_instance_id"); ok { |
| 47 | + tmp := opaInstanceId.(string) |
| 48 | + request.OpaInstanceId = &tmp |
| 49 | + } |
| 50 | + |
| 51 | + request.RequestMetadata.RetryPolicy = tfresource.GetRetryPolicy(false, "opa") |
| 52 | + |
| 53 | + response, err := s.Client.GetOpaInstance(context.Background(), request) |
| 54 | + if err != nil { |
| 55 | + return err |
| 56 | + } |
| 57 | + |
| 58 | + s.Res = &response |
| 59 | + return nil |
| 60 | +} |
| 61 | + |
| 62 | +func (s *OpaOpaInstanceDataSourceCrud) SetData() error { |
| 63 | + if s.Res == nil { |
| 64 | + return nil |
| 65 | + } |
| 66 | + |
| 67 | + s.D.SetId(*s.Res.Id) |
| 68 | + |
| 69 | + if s.Res.CompartmentId != nil { |
| 70 | + s.D.Set("compartment_id", *s.Res.CompartmentId) |
| 71 | + } |
| 72 | + |
| 73 | + s.D.Set("consumption_model", s.Res.ConsumptionModel) |
| 74 | + |
| 75 | + if s.Res.DefinedTags != nil { |
| 76 | + s.D.Set("defined_tags", tfresource.DefinedTagsToMap(s.Res.DefinedTags)) |
| 77 | + } |
| 78 | + |
| 79 | + if s.Res.Description != nil { |
| 80 | + s.D.Set("description", *s.Res.Description) |
| 81 | + } |
| 82 | + |
| 83 | + if s.Res.DisplayName != nil { |
| 84 | + s.D.Set("display_name", *s.Res.DisplayName) |
| 85 | + } |
| 86 | + |
| 87 | + s.D.Set("freeform_tags", s.Res.FreeformTags) |
| 88 | + s.D.Set("freeform_tags", s.Res.FreeformTags) |
| 89 | + |
| 90 | + if s.Res.IdentityAppDisplayName != nil { |
| 91 | + s.D.Set("identity_app_display_name", *s.Res.IdentityAppDisplayName) |
| 92 | + } |
| 93 | + |
| 94 | + if s.Res.IdentityAppGuid != nil { |
| 95 | + s.D.Set("identity_app_guid", *s.Res.IdentityAppGuid) |
| 96 | + } |
| 97 | + |
| 98 | + if s.Res.IdentityAppOpcServiceInstanceGuid != nil { |
| 99 | + s.D.Set("identity_app_opc_service_instance_guid", *s.Res.IdentityAppOpcServiceInstanceGuid) |
| 100 | + } |
| 101 | + |
| 102 | + if s.Res.IdentityDomainUrl != nil { |
| 103 | + s.D.Set("identity_domain_url", *s.Res.IdentityDomainUrl) |
| 104 | + } |
| 105 | + |
| 106 | + if s.Res.InstanceUrl != nil { |
| 107 | + s.D.Set("instance_url", *s.Res.InstanceUrl) |
| 108 | + } |
| 109 | + |
| 110 | + if s.Res.IsBreakglassEnabled != nil { |
| 111 | + s.D.Set("is_breakglass_enabled", *s.Res.IsBreakglassEnabled) |
| 112 | + } |
| 113 | + |
| 114 | + s.D.Set("metering_type", s.Res.MeteringType) |
| 115 | + |
| 116 | + s.D.Set("shape_name", s.Res.ShapeName) |
| 117 | + |
| 118 | + s.D.Set("state", s.Res.LifecycleState) |
| 119 | + |
| 120 | + if s.Res.SystemTags != nil { |
| 121 | + s.D.Set("system_tags", tfresource.SystemTagsToMap(s.Res.SystemTags)) |
| 122 | + } |
| 123 | + |
| 124 | + if s.Res.TimeCreated != nil { |
| 125 | + s.D.Set("time_created", s.Res.TimeCreated.String()) |
| 126 | + } |
| 127 | + |
| 128 | + if s.Res.TimeUpdated != nil { |
| 129 | + s.D.Set("time_updated", s.Res.TimeUpdated.String()) |
| 130 | + } |
| 131 | + |
| 132 | + return nil |
| 133 | +} |
0 commit comments