Skip to content
This repository was archived by the owner on Mar 1, 2023. It is now read-only.

Commit 2a3c71b

Browse files
authored
Extend storage (#35)
* ~ extend updating resources * ~ fix creating sftp storage keys * + data sources for storage s3 & sftp * + data sources for storage sftp key + tests + examples + docs
1 parent d22d6c9 commit 2a3c71b

21 files changed

+906
-77
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "gcore_storage_s3 Data Source - terraform-provider-gcorelabs"
4+
subcategory: ""
5+
description: |-
6+
Represent s3 storage resource. https://storage.gcorelabs.com/storage/list
7+
---
8+
9+
# gcore_storage_s3 (Data Source)
10+
11+
Represent s3 storage resource. https://storage.gcorelabs.com/storage/list
12+
13+
## Example Usage
14+
15+
```terraform
16+
provider gcore {
17+
user_name = "test"
18+
password = "test"
19+
permanent_api_token="123$321" // https://support.gcorelabs.com/hc/en-us/articles/360018625617-API-tokens
20+
ignore_creds_auth_error=true // if you want to manage storage resource only and provide permanent_api_token without user_name & password
21+
gcore_platform = "https://api.gcdn.co"
22+
gcore_storage_api = "https://storage.gcorelabs.com/api"
23+
}
24+
25+
data "gcore_storage_s3" "example_s3" {
26+
name = "example"
27+
}
28+
```
29+
30+
<!-- schema generated by tfplugindocs -->
31+
## Schema
32+
33+
### Optional
34+
35+
- **id** (String) The ID of this resource.
36+
- **name** (String) A name of new storage resource.
37+
- **storage_id** (Number) An id of new storage resource.
38+
39+
### Read-Only
40+
41+
- **client_id** (Number) An client id of new storage resource.
42+
- **generated_endpoint** (String) A s3 entry point for new storage resource.
43+
- **generated_http_endpoint** (String) A http s3 entry point for new storage resource.
44+
- **generated_s3_endpoint** (String) A s3 endpoint for new storage resource.
45+
- **location** (String) A location of new storage resource. One of (s-ed1, s-darz1, s-ws1)
46+
47+
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "gcore_storage_sftp Data Source - terraform-provider-gcorelabs"
4+
subcategory: ""
5+
description: |-
6+
Represent sftp storage resource. https://storage.gcorelabs.com/storage/list
7+
---
8+
9+
# gcore_storage_sftp (Data Source)
10+
11+
Represent sftp storage resource. https://storage.gcorelabs.com/storage/list
12+
13+
## Example Usage
14+
15+
```terraform
16+
provider gcore {
17+
user_name = "test"
18+
password = "test"
19+
permanent_api_token="123$321" // https://support.gcorelabs.com/hc/en-us/articles/360018625617-API-tokens
20+
ignore_creds_auth_error=true // if you want to manage storage resource only and provide permanent_api_token without user_name & password
21+
gcore_platform = "https://api.gcdn.co"
22+
gcore_storage_api = "https://storage.gcorelabs.com/api"
23+
}
24+
25+
data "gcore_storage_sftp" "example_sftp" {
26+
name = "example"
27+
}
28+
```
29+
30+
<!-- schema generated by tfplugindocs -->
31+
## Schema
32+
33+
### Optional
34+
35+
- **id** (String) The ID of this resource.
36+
- **name** (String) A name of storage resource.
37+
- **storage_id** (Number) An id of storage resource.
38+
39+
### Read-Only
40+
41+
- **client_id** (Number) An client id of storage resource.
42+
- **generated_http_endpoint** (String) A http sftp entry point for new storage resource.
43+
- **generated_sftp_endpoint** (String) A ssh sftp entry point for new storage resource.
44+
- **http_expires_header_value** (String) A expires date of storage resource.
45+
- **http_servername_alias** (String) An alias of storage resource.
46+
- **location** (String) A location of new storage resource. One of (ams, sin, fra, mia)
47+
- **ssh_key_id** (List of Number) An ssh keys IDs to link with new sftp storage resource only. https://storage.gcorelabs.com/ssh-key/list
48+
49+
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "gcore_storage_sftp_key Data Source - terraform-provider-gcorelabs"
4+
subcategory: ""
5+
description: |-
6+
Represent storage key resource. https://storage.gcorelabs.com/ssh-key/list
7+
---
8+
9+
# gcore_storage_sftp_key (Data Source)
10+
11+
Represent storage key resource. https://storage.gcorelabs.com/ssh-key/list
12+
13+
## Example Usage
14+
15+
```terraform
16+
provider gcore {
17+
user_name = "test"
18+
password = "test"
19+
permanent_api_token="123$321" // https://support.gcorelabs.com/hc/en-us/articles/360018625617-API-tokens
20+
ignore_creds_auth_error=true // if you want to manage storage resource only and provide permanent_api_token without user_name & password
21+
gcore_platform = "https://api.gcdn.co"
22+
gcore_storage_api = "https://storage.gcorelabs.com/api"
23+
}
24+
25+
data "gcore_storage_sftp_key" "example_key" {
26+
name = "example"
27+
}
28+
```
29+
30+
<!-- schema generated by tfplugindocs -->
31+
## Schema
32+
33+
### Required
34+
35+
- **name** (String) A name of storage key resource.
36+
37+
### Optional
38+
39+
- **id** (String) The ID of this resource.
40+
41+
### Read-Only
42+
43+
- **key_id** (Number) An id of of storage key resource.
44+
45+

docs/resources/gcore_storage_s3.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ resource "gcore_storage_s3" "example_s3" {
4141
- **client_id** (Number) An client id of new storage resource.
4242
- **generated_access_key** (String) A s3 access key for new storage resource.
4343
- **generated_endpoint** (String) A s3 entry point for new storage resource.
44+
- **generated_http_endpoint** (String) A http s3 entry point for new storage resource.
45+
- **generated_s3_endpoint** (String) A s3 endpoint for new storage resource.
4446
- **generated_secret_key** (String) A s3 secret key for new storage resource.
4547
- **id** (String) The ID of this resource.
4648
- **storage_id** (Number) An id of new storage resource.

docs/resources/gcore_storage_sftp.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,15 @@ resource "gcore_storage_sftp" "example_sftp" {
4040
### Optional
4141

4242
- **client_id** (Number) An client id of new storage resource.
43-
- **generated_endpoint** (String) A sftp entry point for new storage resource.
43+
- **generated_http_endpoint** (String) A http sftp entry point for new storage resource.
4444
- **generated_password** (Boolean) An auto generated sftp password for new storage resource.
45+
- **generated_sftp_endpoint** (String) A ssh sftp entry point for new storage resource.
4546
- **http_expires_header_value** (String) A expires date of storage resource.
4647
- **http_servername_alias** (String) An alias of storage resource.
4748
- **id** (String) The ID of this resource.
4849
- **password** (String) A sftp password for new storage resource.
4950
- **ssh_key_id** (List of Number) An ssh keys IDs to link with new sftp storage resource only. https://storage.gcorelabs.com/ssh-key/list
5051
- **storage_id** (Number) An id of new storage resource.
52+
- **update_after_create** (Boolean) A temporary flag. An internal cheat, to skip update ssh keys. Skip it.
5153

5254

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
provider gcore {
2+
user_name = "test"
3+
password = "test"
4+
permanent_api_token="123$321" // https://support.gcorelabs.com/hc/en-us/articles/360018625617-API-tokens
5+
ignore_creds_auth_error=true // if you want to manage storage resource only and provide permanent_api_token without user_name & password
6+
gcore_platform = "https://api.gcdn.co"
7+
gcore_storage_api = "https://storage.gcorelabs.com/api"
8+
}
9+
10+
data "gcore_storage_s3" "example_s3" {
11+
name = "example"
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
provider gcore {
2+
user_name = "test"
3+
password = "test"
4+
permanent_api_token="123$321" // https://support.gcorelabs.com/hc/en-us/articles/360018625617-API-tokens
5+
ignore_creds_auth_error=true // if you want to manage storage resource only and provide permanent_api_token without user_name & password
6+
gcore_platform = "https://api.gcdn.co"
7+
gcore_storage_api = "https://storage.gcorelabs.com/api"
8+
}
9+
10+
data "gcore_storage_sftp" "example_sftp" {
11+
name = "example"
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
provider gcore {
2+
user_name = "test"
3+
password = "test"
4+
permanent_api_token="123$321" // https://support.gcorelabs.com/hc/en-us/articles/360018625617-API-tokens
5+
ignore_creds_auth_error=true // if you want to manage storage resource only and provide permanent_api_token without user_name & password
6+
gcore_platform = "https://api.gcdn.co"
7+
gcore_storage_api = "https://storage.gcorelabs.com/api"
8+
}
9+
10+
data "gcore_storage_sftp_key" "example_key" {
11+
name = "example"
12+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package gcore
2+
3+
import (
4+
"github.com/hashicorp/go-cty/cty"
5+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
7+
"regexp"
8+
)
9+
10+
func dataSourceStorageS3() *schema.Resource {
11+
return &schema.Resource{
12+
Schema: map[string]*schema.Schema{
13+
StorageSchemaId: {
14+
Type: schema.TypeInt,
15+
Optional: true,
16+
AtLeastOneOf: []string{
17+
StorageSchemaId,
18+
StorageSchemaName,
19+
},
20+
Description: "An id of new storage resource.",
21+
},
22+
StorageSchemaClientId: {
23+
Type: schema.TypeInt,
24+
Computed: true,
25+
Description: "An client id of new storage resource.",
26+
},
27+
StorageSchemaName: {
28+
Type: schema.TypeString,
29+
Optional: true,
30+
ValidateDiagFunc: func(i interface{}, path cty.Path) diag.Diagnostics {
31+
storageName := i.(string)
32+
if !regexp.MustCompile(`^[\w\-]+$`).MatchString(storageName) || len(storageName) > 255 {
33+
return diag.Errorf("storage name can't be empty and can have only letters, numbers, dashes and underscores, it also should be less than 256 symbols")
34+
}
35+
return nil
36+
},
37+
AtLeastOneOf: []string{
38+
StorageSchemaId,
39+
StorageSchemaName,
40+
},
41+
Description: "A name of new storage resource.",
42+
},
43+
StorageSchemaLocation: {
44+
Type: schema.TypeString,
45+
Computed: true,
46+
Description: "A location of new storage resource. One of (s-ed1, s-darz1, s-ws1)",
47+
},
48+
StorageSchemaGenerateHTTPEndpoint: {
49+
Type: schema.TypeString,
50+
Computed: true,
51+
Description: "A http s3 entry point for new storage resource.",
52+
},
53+
StorageSchemaGenerateS3Endpoint: {
54+
Type: schema.TypeString,
55+
Computed: true,
56+
Description: "A s3 endpoint for new storage resource.",
57+
},
58+
StorageSchemaGenerateEndpoint: {
59+
Type: schema.TypeString,
60+
Computed: true,
61+
Description: "A s3 entry point for new storage resource.",
62+
},
63+
},
64+
ReadContext: resourceStorageS3Read,
65+
Description: "Represent s3 storage resource. https://storage.gcorelabs.com/storage/list",
66+
}
67+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package gcore
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"github.com/G-Core/gcorelabs-storage-sdk-go/swagger/client/storage"
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
9+
"testing"
10+
"time"
11+
)
12+
13+
func TestStorageS3DataSource(t *testing.T) {
14+
random := time.Now().Nanosecond()
15+
resourceName := fmt.Sprintf("gcore_storage_s3.terraformtest%d_s3", random)
16+
dataSourceName := fmt.Sprintf("data.gcore_storage_s3.terraformtest%d_s3_data", random)
17+
18+
templateCreate := func() string {
19+
return fmt.Sprintf(`
20+
resource "gcore_storage_s3" "terraformtest%d_s3" {
21+
name = "terraformtest%d"
22+
location = "s-ed1"
23+
}
24+
`, random, random)
25+
}
26+
27+
templateRead := func() string {
28+
return fmt.Sprintf(`
29+
data "gcore_storage_s3" "terraformtest%d_s3_data" {
30+
name = "terraformtest%d"
31+
}
32+
`, random, random)
33+
}
34+
35+
resource.Test(t, resource.TestCase{
36+
PreCheck: func() {
37+
testAccPreCheckVars(t, GCORE_USERNAME_VAR, GCORE_PASSWORD_VAR, GCORE_STORAGE_URL_VAR)
38+
},
39+
CheckDestroy: func(s *terraform.State) error {
40+
config := testAccProvider.Meta().(*Config)
41+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
42+
defer cancel()
43+
44+
for _, rs := range s.RootModule().Resources {
45+
if rs.Type != "gcore_storage_s3" {
46+
continue
47+
}
48+
opts := []func(opt *storage.StorageListHTTPV2Params){
49+
func(opt *storage.StorageListHTTPV2Params) { opt.Context = ctx },
50+
func(opt *storage.StorageListHTTPV2Params) { opt.ID = &rs.Primary.ID },
51+
}
52+
storages, err := config.StorageClient.StoragesList(opts...)
53+
if err != nil {
54+
return fmt.Errorf("find storage: %w", err)
55+
}
56+
if len(storages) == 0 {
57+
return nil
58+
}
59+
if storages[0].ProvisioningStatus == "ok" {
60+
return fmt.Errorf("storage #%s wasn't deleted correctrly", rs.Primary.ID)
61+
}
62+
}
63+
return nil
64+
},
65+
ProviderFactories: testAccProviders,
66+
Steps: []resource.TestStep{
67+
{
68+
Config: templateCreate(),
69+
Check: resource.ComposeTestCheckFunc(
70+
testAccCheckResourceExists(resourceName),
71+
resource.TestCheckResourceAttr(resourceName, StorageSchemaLocation, "s-ed1"),
72+
),
73+
},
74+
{
75+
Config: templateRead(),
76+
Check: resource.ComposeTestCheckFunc(
77+
testAccCheckResourceExists(dataSourceName),
78+
resource.TestCheckResourceAttr(dataSourceName, StorageSchemaLocation, "s-ed1"),
79+
),
80+
},
81+
},
82+
})
83+
}

0 commit comments

Comments
 (0)