|
| 1 | +// Copyright IBM Corp. 2017, 2021 All Rights Reserved. |
| 2 | +// Licensed under the Mozilla Public License v2.0 |
| 3 | + |
| 4 | +package kms |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + |
| 9 | + "github.com/IBM-Cloud/terraform-provider-ibm/ibm/validate" |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 11 | +) |
| 12 | + |
| 13 | +func dataSourceIBMKMSKmipAdapterBaseSchema() map[string]*schema.Schema { |
| 14 | + return map[string]*schema.Schema{ |
| 15 | + "adapter_id": { |
| 16 | + Type: schema.TypeString, |
| 17 | + Computed: true, |
| 18 | + Description: "The UUID of the KMIP adapter to be fetched", |
| 19 | + }, |
| 20 | + "name": { |
| 21 | + Type: schema.TypeString, |
| 22 | + Computed: true, |
| 23 | + Description: "The name of the KMIP adapter to be fetched", |
| 24 | + }, |
| 25 | + "description": { |
| 26 | + Type: schema.TypeString, |
| 27 | + Computed: true, |
| 28 | + Description: "The description of the KMIP adapter to be fetched", |
| 29 | + }, |
| 30 | + "profile": { |
| 31 | + Type: schema.TypeString, |
| 32 | + Computed: true, |
| 33 | + Description: "The profile of the KMIP adapter to be fetched", |
| 34 | + }, |
| 35 | + "profile_data": { |
| 36 | + Type: schema.TypeMap, |
| 37 | + Computed: true, |
| 38 | + Description: "The data specific to the KMIP Adapter profile", |
| 39 | + }, |
| 40 | + "created_by": &schema.Schema{ |
| 41 | + Type: schema.TypeString, |
| 42 | + Computed: true, |
| 43 | + Description: "The unique identifier that is associated with the entity that created the adapter.", |
| 44 | + }, |
| 45 | + "created_at": &schema.Schema{ |
| 46 | + Type: schema.TypeString, |
| 47 | + Computed: true, |
| 48 | + Description: "The date when a resource was created. The date format follows RFC 3339.", |
| 49 | + }, |
| 50 | + "updated_by": &schema.Schema{ |
| 51 | + Type: schema.TypeString, |
| 52 | + Computed: true, |
| 53 | + Description: "The unique identifier that is associated with the entity that updated the adapter.", |
| 54 | + }, |
| 55 | + "updated_at": &schema.Schema{ |
| 56 | + Type: schema.TypeString, |
| 57 | + Computed: true, |
| 58 | + Description: "The date when a resource was updated. The date format follows RFC 3339.", |
| 59 | + }, |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +func DataSourceIBMKMSKmipAdapter() *schema.Resource { |
| 64 | + baseMap := dataSourceIBMKMSKmipAdapterBaseSchema() |
| 65 | + |
| 66 | + baseMap["endpoint_type"] = &schema.Schema{ |
| 67 | + Type: schema.TypeString, |
| 68 | + Optional: true, |
| 69 | + Computed: true, |
| 70 | + ValidateFunc: validate.ValidateAllowedStringValues([]string{"public", "private"}), |
| 71 | + Description: "public or private", |
| 72 | + } |
| 73 | + |
| 74 | + baseMap["instance_id"] = &schema.Schema{ |
| 75 | + Type: schema.TypeString, |
| 76 | + Required: true, |
| 77 | + Description: "Key protect or hpcs instance GUID", |
| 78 | + DiffSuppressFunc: suppressKMSInstanceIDDiff, |
| 79 | + } |
| 80 | + adapterIDSchema := baseMap["adapter_id"] |
| 81 | + adapterIDSchema.Optional = true |
| 82 | + adapterIDSchema.ExactlyOneOf = []string{"adapter_id", "name"} |
| 83 | + baseMap["adapter_id"] = adapterIDSchema |
| 84 | + |
| 85 | + adapterNameSchema := baseMap["name"] |
| 86 | + adapterNameSchema.Optional = true |
| 87 | + adapterNameSchema.ExactlyOneOf = []string{"adapter_id", "name"} |
| 88 | + baseMap["name"] = adapterNameSchema |
| 89 | + |
| 90 | + return &schema.Resource{ |
| 91 | + Read: dataSourceIBMKMSKmipAdapterRead, |
| 92 | + Schema: baseMap, |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +func dataSourceIBMKMSKmipAdapterRead(d *schema.ResourceData, meta interface{}) error { |
| 97 | + // initialize API |
| 98 | + instanceID := getInstanceIDFromResourceData(d, "instance_id") |
| 99 | + kpAPI, _, err := populateKPClient(d, meta, instanceID) |
| 100 | + if err != nil { |
| 101 | + return err |
| 102 | + } |
| 103 | + |
| 104 | + nameOrID, hasID := d.GetOk("adapter_id") |
| 105 | + if !hasID { |
| 106 | + nameOrID = d.Get("name") |
| 107 | + } |
| 108 | + adapterNameOrID := nameOrID.(string) |
| 109 | + // call GetKMIPAdapter api |
| 110 | + adapter, err := kpAPI.GetKMIPAdapter(context.Background(), adapterNameOrID) |
| 111 | + if err != nil { |
| 112 | + return err |
| 113 | + } |
| 114 | + |
| 115 | + // set computed values |
| 116 | + return populateKMIPAdapterSchemaDataFromStruct(d, *adapter, instanceID) |
| 117 | +} |
0 commit comments