|
4 | 4 | package oci |
5 | 5 |
|
6 | 6 | import ( |
| 7 | + "bytes" |
7 | 8 | "context" |
| 9 | + "encoding/base64" |
8 | 10 | "fmt" |
| 11 | + "io/ioutil" |
9 | 12 | "log" |
| 13 | + "strconv" |
10 | 14 | "time" |
11 | 15 |
|
12 | 16 | "github.com/hashicorp/terraform/helper/schema" |
@@ -104,34 +108,12 @@ func KmsKeyResource() *schema.Resource { |
104 | 108 | Computed: true, |
105 | 109 | Optional: true, |
106 | 110 | }, |
107 | | - |
108 | | - // Computed |
109 | | - "current_key_version": { |
110 | | - Type: schema.TypeString, |
111 | | - Computed: true, |
112 | | - }, |
113 | | - "restored_from_key_id": { |
114 | | - Type: schema.TypeString, |
115 | | - Computed: true, |
116 | | - }, |
117 | | - "state": { |
118 | | - Type: schema.TypeString, |
119 | | - Computed: true, |
120 | | - }, |
121 | | - "time_created": { |
122 | | - Type: schema.TypeString, |
123 | | - Computed: true, |
124 | | - }, |
125 | | - "vault_id": { |
126 | | - Type: schema.TypeString, |
127 | | - Computed: true, |
128 | | - }, |
129 | | - |
130 | 111 | "restore_from_object_store": { |
131 | | - Type: schema.TypeList, |
132 | | - Optional: true, |
133 | | - MaxItems: 1, |
134 | | - MinItems: 1, |
| 112 | + Type: schema.TypeList, |
| 113 | + Optional: true, |
| 114 | + MaxItems: 1, |
| 115 | + MinItems: 1, |
| 116 | + ConflictsWith: []string{"restore_from_file"}, |
135 | 117 | Elem: &schema.Resource{ |
136 | 118 | Schema: map[string]*schema.Schema{ |
137 | 119 | // Required |
@@ -167,10 +149,62 @@ func KmsKeyResource() *schema.Resource { |
167 | 149 | }, |
168 | 150 | }, |
169 | 151 | }, |
| 152 | + "restore_from_file": { |
| 153 | + Type: schema.TypeList, |
| 154 | + Optional: true, |
| 155 | + MaxItems: 1, |
| 156 | + MinItems: 1, |
| 157 | + ConflictsWith: []string{"restore_from_object_store"}, |
| 158 | + Elem: &schema.Resource{ |
| 159 | + Schema: map[string]*schema.Schema{ |
| 160 | + // Required |
| 161 | + "restore_key_from_file_details": { |
| 162 | + Type: schema.TypeString, |
| 163 | + Required: true, |
| 164 | + }, |
| 165 | + "content_length": { |
| 166 | + Type: schema.TypeString, |
| 167 | + Required: true, |
| 168 | + ValidateFunc: validateInt64TypeString, |
| 169 | + DiffSuppressFunc: int64StringDiffSuppressFunction, |
| 170 | + }, |
| 171 | + |
| 172 | + // Optional |
| 173 | + "content_md5": { |
| 174 | + Type: schema.TypeString, |
| 175 | + Optional: true, |
| 176 | + }, |
| 177 | + |
| 178 | + // Computed |
| 179 | + }, |
| 180 | + }, |
| 181 | + }, |
170 | 182 | "restore_trigger": { |
171 | 183 | Type: schema.TypeBool, |
172 | 184 | Optional: true, |
173 | 185 | }, |
| 186 | + |
| 187 | + // Computed |
| 188 | + "current_key_version": { |
| 189 | + Type: schema.TypeString, |
| 190 | + Computed: true, |
| 191 | + }, |
| 192 | + "restored_from_key_id": { |
| 193 | + Type: schema.TypeString, |
| 194 | + Computed: true, |
| 195 | + }, |
| 196 | + "state": { |
| 197 | + Type: schema.TypeString, |
| 198 | + Computed: true, |
| 199 | + }, |
| 200 | + "time_created": { |
| 201 | + Type: schema.TypeString, |
| 202 | + Computed: true, |
| 203 | + }, |
| 204 | + "vault_id": { |
| 205 | + Type: schema.TypeString, |
| 206 | + Computed: true, |
| 207 | + }, |
174 | 208 | }, |
175 | 209 | } |
176 | 210 | } |
@@ -316,7 +350,15 @@ func (s *KmsKeyResourceCrud) UpdatedTarget() []string { |
316 | 350 | } |
317 | 351 |
|
318 | 352 | func (s *KmsKeyResourceCrud) Create() error { |
319 | | - if _, ok := s.D.GetOkExists("restore_from_object_store"); ok { |
| 353 | + if _, ok := s.D.GetOk("restore_from_file"); ok { |
| 354 | + err := s.RestoreKeyFromFile() |
| 355 | + if err != nil { |
| 356 | + return err |
| 357 | + } |
| 358 | + s.D.SetId(s.ID()) |
| 359 | + return s.UpdateKeyDetails() |
| 360 | + } |
| 361 | + if _, ok := s.D.GetOk("restore_from_object_store"); ok { |
320 | 362 | err := s.RestoreKeyFromObjectStore() |
321 | 363 | if err != nil { |
322 | 364 | return err |
@@ -394,7 +436,14 @@ func (s *KmsKeyResourceCrud) Get() error { |
394 | 436 | } |
395 | 437 |
|
396 | 438 | func (s *KmsKeyResourceCrud) Update() error { |
397 | | - if _, ok := s.D.GetOkExists("restore_from_object_store"); ok && s.D.HasChange("restore_trigger") { |
| 439 | + if _, ok := s.D.GetOk("restore_from_file"); ok && s.D.HasChange("restore_trigger") { |
| 440 | + err := s.RestoreKeyFromFile() |
| 441 | + if err != nil { |
| 442 | + return err |
| 443 | + } |
| 444 | + s.D.SetId(s.ID()) |
| 445 | + } |
| 446 | + if _, ok := s.D.GetOk("restore_from_object_store"); ok && s.D.HasChange("restore_trigger") { |
398 | 447 | err := s.RestoreKeyFromObjectStore() |
399 | 448 | if err != nil { |
400 | 449 | return err |
@@ -611,6 +660,40 @@ func (s *KmsKeyResourceCrud) RestoreKeyFromObjectStore() error { |
611 | 660 | return nil |
612 | 661 | } |
613 | 662 |
|
| 663 | +func (s *KmsKeyResourceCrud) RestoreKeyFromFile() error { |
| 664 | + request := oci_kms.RestoreKeyFromFileRequest{} |
| 665 | + if restoreKeyFromFileDetails, ok := s.D.GetOk("restore_from_file.0.restore_key_from_file_details"); ok { |
| 666 | + decodedFileContent, _ := base64.StdEncoding.DecodeString(restoreKeyFromFileDetails.(string)) |
| 667 | + request.RestoreKeyFromFileDetails = ioutil.NopCloser(bytes.NewBuffer(decodedFileContent)) |
| 668 | + } else { |
| 669 | + request.RestoreKeyFromFileDetails = ioutil.NopCloser(bytes.NewBuffer([]byte{})) |
| 670 | + } |
| 671 | + |
| 672 | + if contentLength, ok := s.D.GetOk("restore_from_file.0.content_length"); ok { |
| 673 | + tmp := contentLength.(string) |
| 674 | + tmpInt64, err := strconv.ParseInt(tmp, 10, 64) |
| 675 | + if err != nil { |
| 676 | + return fmt.Errorf("unable to convert content-length string: %s to an int64 and encountered error: %v", tmp, err) |
| 677 | + } |
| 678 | + request.ContentLength = &tmpInt64 |
| 679 | + } |
| 680 | + |
| 681 | + if contentMd5, ok := s.D.GetOk("restore_from_file.0.content_md5"); ok { |
| 682 | + tmp := contentMd5.(string) |
| 683 | + request.ContentMd5 = &tmp |
| 684 | + } |
| 685 | + |
| 686 | + request.RequestMetadata.RetryPolicy = getRetryPolicy(s.DisableNotFoundRetries, "kms") |
| 687 | + |
| 688 | + response, err := s.Client.RestoreKeyFromFile(context.Background(), request) |
| 689 | + if err != nil { |
| 690 | + return err |
| 691 | + } |
| 692 | + |
| 693 | + s.Res = &response.Key |
| 694 | + return nil |
| 695 | +} |
| 696 | + |
614 | 697 | func (s *KmsKeyResourceCrud) mapToBackupLocation(fieldKeyFormat string) (oci_kms.BackupLocation, error) { |
615 | 698 | var baseObject oci_kms.BackupLocation |
616 | 699 | //discriminator |
|
0 commit comments