|
| 1 | +package gcore |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "log" |
| 6 | + |
| 7 | + gcorecloud "github.com/G-Core/gcorelabscloud-go" |
| 8 | + "github.com/G-Core/gcorelabscloud-go/gcore/secret/v1/secrets" |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 11 | +) |
| 12 | + |
| 13 | +func dataSourceSecret() *schema.Resource { |
| 14 | + return &schema.Resource{ |
| 15 | + ReadContext: dataSourceSecretRead, |
| 16 | + Description: "Represent secret", |
| 17 | + Schema: map[string]*schema.Schema{ |
| 18 | + "project_id": &schema.Schema{ |
| 19 | + Type: schema.TypeInt, |
| 20 | + Optional: true, |
| 21 | + ExactlyOneOf: []string{ |
| 22 | + "project_id", |
| 23 | + "project_name", |
| 24 | + }, |
| 25 | + }, |
| 26 | + "region_id": &schema.Schema{ |
| 27 | + Type: schema.TypeInt, |
| 28 | + Optional: true, |
| 29 | + ExactlyOneOf: []string{ |
| 30 | + "region_id", |
| 31 | + "region_name", |
| 32 | + }, |
| 33 | + }, |
| 34 | + "project_name": &schema.Schema{ |
| 35 | + Type: schema.TypeString, |
| 36 | + Optional: true, |
| 37 | + ExactlyOneOf: []string{ |
| 38 | + "project_id", |
| 39 | + "project_name", |
| 40 | + }, |
| 41 | + }, |
| 42 | + "region_name": &schema.Schema{ |
| 43 | + Type: schema.TypeString, |
| 44 | + Optional: true, |
| 45 | + ExactlyOneOf: []string{ |
| 46 | + "region_id", |
| 47 | + "region_name", |
| 48 | + }, |
| 49 | + }, |
| 50 | + "name": &schema.Schema{ |
| 51 | + Type: schema.TypeString, |
| 52 | + Required: true, |
| 53 | + }, |
| 54 | + "algorithm": &schema.Schema{ |
| 55 | + Type: schema.TypeString, |
| 56 | + Computed: true, |
| 57 | + }, |
| 58 | + "bit_length": &schema.Schema{ |
| 59 | + Type: schema.TypeInt, |
| 60 | + Computed: true, |
| 61 | + }, |
| 62 | + "mode": &schema.Schema{ |
| 63 | + Type: schema.TypeString, |
| 64 | + Computed: true, |
| 65 | + }, |
| 66 | + "status": &schema.Schema{ |
| 67 | + Type: schema.TypeString, |
| 68 | + Computed: true, |
| 69 | + }, |
| 70 | + "content_types": &schema.Schema{ |
| 71 | + Type: schema.TypeMap, |
| 72 | + Computed: true, |
| 73 | + Elem: &schema.Schema{ |
| 74 | + Type: schema.TypeString, |
| 75 | + }, |
| 76 | + }, |
| 77 | + "expiration": &schema.Schema{ |
| 78 | + Type: schema.TypeString, |
| 79 | + Description: "Datetime when the secret will expire. The format is 2025-12-28T19:14:44.180394", |
| 80 | + Computed: true, |
| 81 | + }, |
| 82 | + "created": &schema.Schema{ |
| 83 | + Type: schema.TypeString, |
| 84 | + Description: "Datetime when the secret was created. The format is 2025-12-28T19:14:44.180394", |
| 85 | + Computed: true, |
| 86 | + }, |
| 87 | + }, |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +func dataSourceSecretRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { |
| 92 | + log.Println("[DEBUG] Start secret reading") |
| 93 | + var diags diag.Diagnostics |
| 94 | + config := m.(*Config) |
| 95 | + provider := config.Provider |
| 96 | + secretID := d.Id() |
| 97 | + log.Printf("[DEBUG] Secret id = %s", secretID) |
| 98 | + |
| 99 | + client, err := CreateClient(provider, d, secretPoint, versionPointV1) |
| 100 | + if err != nil { |
| 101 | + return diag.FromErr(err) |
| 102 | + } |
| 103 | + |
| 104 | + allSecrets, err := secrets.ListAll(client) |
| 105 | + if err != nil { |
| 106 | + return diag.Errorf("cannot get secrets. Error: %s", err.Error()) |
| 107 | + } |
| 108 | + |
| 109 | + var found bool |
| 110 | + name := d.Get("name").(string) |
| 111 | + for _, secret := range allSecrets { |
| 112 | + if name == secret.Name { |
| 113 | + d.SetId(secret.ID) |
| 114 | + d.Set("name", name) |
| 115 | + d.Set("algorithm", secret.Algorithm) |
| 116 | + d.Set("bit_length", secret.BitLength) |
| 117 | + d.Set("mode", secret.Mode) |
| 118 | + d.Set("status", secret.Status) |
| 119 | + d.Set("expiration", secret.Expiration.Format(gcorecloud.RFC3339ZColon)) |
| 120 | + d.Set("created", secret.CreatedAt.Format(gcorecloud.RFC3339ZColon)) |
| 121 | + if err := d.Set("content_types", secret.ContentTypes); err != nil { |
| 122 | + return diag.FromErr(err) |
| 123 | + } |
| 124 | + found = true |
| 125 | + break |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + if !found { |
| 130 | + return diag.Errorf("secret with name %s does not exit", name) |
| 131 | + } |
| 132 | + |
| 133 | + log.Println("[DEBUG] Finish secret reading") |
| 134 | + return diags |
| 135 | +} |
0 commit comments