|
| 1 | +// Copyright 2024 StreamNative, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package cloud |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + "strings" |
| 21 | + |
| 22 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 23 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 24 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 25 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 26 | +) |
| 27 | + |
| 28 | +func dataSourceSecret() *schema.Resource { |
| 29 | + return &schema.Resource{ |
| 30 | + ReadContext: dataSourceSecretRead, |
| 31 | + Importer: &schema.ResourceImporter{ |
| 32 | + StateContext: func( |
| 33 | + ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { |
| 34 | + parts := strings.Split(d.Id(), "/") |
| 35 | + if len(parts) != 2 { |
| 36 | + return nil, fmt.Errorf("invalid import id %q, expected <organization>/<name>", d.Id()) |
| 37 | + } |
| 38 | + _ = d.Set("organization", parts[0]) |
| 39 | + _ = d.Set("name", parts[1]) |
| 40 | + if diags := dataSourceSecretRead(ctx, d, meta); diags.HasError() { |
| 41 | + return nil, fmt.Errorf("import %q: %s", d.Id(), diags[0].Summary) |
| 42 | + } |
| 43 | + return []*schema.ResourceData{d}, nil |
| 44 | + }, |
| 45 | + }, |
| 46 | + Schema: map[string]*schema.Schema{ |
| 47 | + "organization": { |
| 48 | + Type: schema.TypeString, |
| 49 | + Required: true, |
| 50 | + Description: descriptions["organization"], |
| 51 | + ValidateFunc: validateNotBlank, |
| 52 | + }, |
| 53 | + "name": { |
| 54 | + Type: schema.TypeString, |
| 55 | + Required: true, |
| 56 | + Description: descriptions["secret_name"], |
| 57 | + ValidateFunc: validateNotBlank, |
| 58 | + }, |
| 59 | + "instance_name": { |
| 60 | + Type: schema.TypeString, |
| 61 | + Computed: true, |
| 62 | + Description: descriptions["instance_name"], |
| 63 | + }, |
| 64 | + "location": { |
| 65 | + Type: schema.TypeString, |
| 66 | + Computed: true, |
| 67 | + Description: descriptions["location"], |
| 68 | + }, |
| 69 | + "pool_member_name": { |
| 70 | + Type: schema.TypeString, |
| 71 | + Computed: true, |
| 72 | + Description: descriptions["pool_member_name"], |
| 73 | + }, |
| 74 | + "type": { |
| 75 | + Type: schema.TypeString, |
| 76 | + Computed: true, |
| 77 | + Description: descriptions["secret_type"], |
| 78 | + }, |
| 79 | + "data": { |
| 80 | + Type: schema.TypeMap, |
| 81 | + Computed: true, |
| 82 | + Sensitive: true, |
| 83 | + Description: descriptions["secret_data"], |
| 84 | + Elem: &schema.Schema{ |
| 85 | + Type: schema.TypeString, |
| 86 | + }, |
| 87 | + }, |
| 88 | + }, |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +func dataSourceSecretRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 93 | + namespace := d.Get("organization").(string) |
| 94 | + name := d.Get("name").(string) |
| 95 | + |
| 96 | + clientSet, err := getClientSet(getFactoryFromMeta(meta)) |
| 97 | + if err != nil { |
| 98 | + return diag.FromErr(fmt.Errorf("ERROR_INIT_CLIENT_ON_READ_SECRET: %w", err)) |
| 99 | + } |
| 100 | + |
| 101 | + secret, err := clientSet.CloudV1alpha1().Secrets(namespace).Get(ctx, name, metav1.GetOptions{}) |
| 102 | + if err != nil { |
| 103 | + if apierrors.IsNotFound(err) { |
| 104 | + d.SetId("") |
| 105 | + return nil |
| 106 | + } |
| 107 | + return diag.FromErr(fmt.Errorf("ERROR_READ_SECRET: %w", err)) |
| 108 | + } |
| 109 | + |
| 110 | + return setSecretState(d, secret) |
| 111 | +} |
0 commit comments