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

Commit

Permalink
Extend storage (#35)
Browse files Browse the repository at this point in the history
* ~ extend updating resources

* ~ fix creating sftp storage keys

* + data sources for storage s3 & sftp

* + data sources for storage sftp key + tests + examples + docs
  • Loading branch information
shubinmi authored Jun 9, 2021
1 parent d22d6c9 commit 2a3c71b
Show file tree
Hide file tree
Showing 21 changed files with 906 additions and 77 deletions.
47 changes: 47 additions & 0 deletions docs/data-sources/gcore_storage_s3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "gcore_storage_s3 Data Source - terraform-provider-gcorelabs"
subcategory: ""
description: |-
Represent s3 storage resource. https://storage.gcorelabs.com/storage/list
---

# gcore_storage_s3 (Data Source)

Represent s3 storage resource. https://storage.gcorelabs.com/storage/list

## Example Usage

```terraform
provider gcore {
user_name = "test"
password = "test"
permanent_api_token="123$321" // https://support.gcorelabs.com/hc/en-us/articles/360018625617-API-tokens
ignore_creds_auth_error=true // if you want to manage storage resource only and provide permanent_api_token without user_name & password
gcore_platform = "https://api.gcdn.co"
gcore_storage_api = "https://storage.gcorelabs.com/api"
}
data "gcore_storage_s3" "example_s3" {
name = "example"
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Optional

- **id** (String) The ID of this resource.
- **name** (String) A name of new storage resource.
- **storage_id** (Number) An id of new storage resource.

### Read-Only

- **client_id** (Number) An client id of new storage resource.
- **generated_endpoint** (String) A s3 entry point for new storage resource.
- **generated_http_endpoint** (String) A http s3 entry point for new storage resource.
- **generated_s3_endpoint** (String) A s3 endpoint for new storage resource.
- **location** (String) A location of new storage resource. One of (s-ed1, s-darz1, s-ws1)


49 changes: 49 additions & 0 deletions docs/data-sources/gcore_storage_sftp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "gcore_storage_sftp Data Source - terraform-provider-gcorelabs"
subcategory: ""
description: |-
Represent sftp storage resource. https://storage.gcorelabs.com/storage/list
---

# gcore_storage_sftp (Data Source)

Represent sftp storage resource. https://storage.gcorelabs.com/storage/list

## Example Usage

```terraform
provider gcore {
user_name = "test"
password = "test"
permanent_api_token="123$321" // https://support.gcorelabs.com/hc/en-us/articles/360018625617-API-tokens
ignore_creds_auth_error=true // if you want to manage storage resource only and provide permanent_api_token without user_name & password
gcore_platform = "https://api.gcdn.co"
gcore_storage_api = "https://storage.gcorelabs.com/api"
}
data "gcore_storage_sftp" "example_sftp" {
name = "example"
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Optional

- **id** (String) The ID of this resource.
- **name** (String) A name of storage resource.
- **storage_id** (Number) An id of storage resource.

### Read-Only

- **client_id** (Number) An client id of storage resource.
- **generated_http_endpoint** (String) A http sftp entry point for new storage resource.
- **generated_sftp_endpoint** (String) A ssh sftp entry point for new storage resource.
- **http_expires_header_value** (String) A expires date of storage resource.
- **http_servername_alias** (String) An alias of storage resource.
- **location** (String) A location of new storage resource. One of (ams, sin, fra, mia)
- **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


45 changes: 45 additions & 0 deletions docs/data-sources/gcore_storage_sftp_key.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "gcore_storage_sftp_key Data Source - terraform-provider-gcorelabs"
subcategory: ""
description: |-
Represent storage key resource. https://storage.gcorelabs.com/ssh-key/list
---

# gcore_storage_sftp_key (Data Source)

Represent storage key resource. https://storage.gcorelabs.com/ssh-key/list

## Example Usage

```terraform
provider gcore {
user_name = "test"
password = "test"
permanent_api_token="123$321" // https://support.gcorelabs.com/hc/en-us/articles/360018625617-API-tokens
ignore_creds_auth_error=true // if you want to manage storage resource only and provide permanent_api_token without user_name & password
gcore_platform = "https://api.gcdn.co"
gcore_storage_api = "https://storage.gcorelabs.com/api"
}
data "gcore_storage_sftp_key" "example_key" {
name = "example"
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- **name** (String) A name of storage key resource.

### Optional

- **id** (String) The ID of this resource.

### Read-Only

- **key_id** (Number) An id of of storage key resource.


2 changes: 2 additions & 0 deletions docs/resources/gcore_storage_s3.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ resource "gcore_storage_s3" "example_s3" {
- **client_id** (Number) An client id of new storage resource.
- **generated_access_key** (String) A s3 access key for new storage resource.
- **generated_endpoint** (String) A s3 entry point for new storage resource.
- **generated_http_endpoint** (String) A http s3 entry point for new storage resource.
- **generated_s3_endpoint** (String) A s3 endpoint for new storage resource.
- **generated_secret_key** (String) A s3 secret key for new storage resource.
- **id** (String) The ID of this resource.
- **storage_id** (Number) An id of new storage resource.
Expand Down
4 changes: 3 additions & 1 deletion docs/resources/gcore_storage_sftp.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,15 @@ resource "gcore_storage_sftp" "example_sftp" {
### Optional

- **client_id** (Number) An client id of new storage resource.
- **generated_endpoint** (String) A sftp entry point for new storage resource.
- **generated_http_endpoint** (String) A http sftp entry point for new storage resource.
- **generated_password** (Boolean) An auto generated sftp password for new storage resource.
- **generated_sftp_endpoint** (String) A ssh sftp entry point for new storage resource.
- **http_expires_header_value** (String) A expires date of storage resource.
- **http_servername_alias** (String) An alias of storage resource.
- **id** (String) The ID of this resource.
- **password** (String) A sftp password for new storage resource.
- **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
- **storage_id** (Number) An id of new storage resource.
- **update_after_create** (Boolean) A temporary flag. An internal cheat, to skip update ssh keys. Skip it.


12 changes: 12 additions & 0 deletions examples/data-sources/gcore_storage_s3/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
provider gcore {
user_name = "test"
password = "test"
permanent_api_token="123$321" // https://support.gcorelabs.com/hc/en-us/articles/360018625617-API-tokens
ignore_creds_auth_error=true // if you want to manage storage resource only and provide permanent_api_token without user_name & password
gcore_platform = "https://api.gcdn.co"
gcore_storage_api = "https://storage.gcorelabs.com/api"
}

data "gcore_storage_s3" "example_s3" {
name = "example"
}
12 changes: 12 additions & 0 deletions examples/data-sources/gcore_storage_sftp/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
provider gcore {
user_name = "test"
password = "test"
permanent_api_token="123$321" // https://support.gcorelabs.com/hc/en-us/articles/360018625617-API-tokens
ignore_creds_auth_error=true // if you want to manage storage resource only and provide permanent_api_token without user_name & password
gcore_platform = "https://api.gcdn.co"
gcore_storage_api = "https://storage.gcorelabs.com/api"
}

data "gcore_storage_sftp" "example_sftp" {
name = "example"
}
12 changes: 12 additions & 0 deletions examples/data-sources/gcore_storage_sftp_key/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
provider gcore {
user_name = "test"
password = "test"
permanent_api_token="123$321" // https://support.gcorelabs.com/hc/en-us/articles/360018625617-API-tokens
ignore_creds_auth_error=true // if you want to manage storage resource only and provide permanent_api_token without user_name & password
gcore_platform = "https://api.gcdn.co"
gcore_storage_api = "https://storage.gcorelabs.com/api"
}

data "gcore_storage_sftp_key" "example_key" {
name = "example"
}
67 changes: 67 additions & 0 deletions gcore/data_source_gcore_storage_s3.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package gcore

import (
"github.com/hashicorp/go-cty/cty"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"regexp"
)

func dataSourceStorageS3() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
StorageSchemaId: {
Type: schema.TypeInt,
Optional: true,
AtLeastOneOf: []string{
StorageSchemaId,
StorageSchemaName,
},
Description: "An id of new storage resource.",
},
StorageSchemaClientId: {
Type: schema.TypeInt,
Computed: true,
Description: "An client id of new storage resource.",
},
StorageSchemaName: {
Type: schema.TypeString,
Optional: true,
ValidateDiagFunc: func(i interface{}, path cty.Path) diag.Diagnostics {
storageName := i.(string)
if !regexp.MustCompile(`^[\w\-]+$`).MatchString(storageName) || len(storageName) > 255 {
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")
}
return nil
},
AtLeastOneOf: []string{
StorageSchemaId,
StorageSchemaName,
},
Description: "A name of new storage resource.",
},
StorageSchemaLocation: {
Type: schema.TypeString,
Computed: true,
Description: "A location of new storage resource. One of (s-ed1, s-darz1, s-ws1)",
},
StorageSchemaGenerateHTTPEndpoint: {
Type: schema.TypeString,
Computed: true,
Description: "A http s3 entry point for new storage resource.",
},
StorageSchemaGenerateS3Endpoint: {
Type: schema.TypeString,
Computed: true,
Description: "A s3 endpoint for new storage resource.",
},
StorageSchemaGenerateEndpoint: {
Type: schema.TypeString,
Computed: true,
Description: "A s3 entry point for new storage resource.",
},
},
ReadContext: resourceStorageS3Read,
Description: "Represent s3 storage resource. https://storage.gcorelabs.com/storage/list",
}
}
83 changes: 83 additions & 0 deletions gcore/data_source_gcore_storage_s3_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package gcore

import (
"context"
"fmt"
"github.com/G-Core/gcorelabs-storage-sdk-go/swagger/client/storage"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"testing"
"time"
)

func TestStorageS3DataSource(t *testing.T) {
random := time.Now().Nanosecond()
resourceName := fmt.Sprintf("gcore_storage_s3.terraformtest%d_s3", random)
dataSourceName := fmt.Sprintf("data.gcore_storage_s3.terraformtest%d_s3_data", random)

templateCreate := func() string {
return fmt.Sprintf(`
resource "gcore_storage_s3" "terraformtest%d_s3" {
name = "terraformtest%d"
location = "s-ed1"
}
`, random, random)
}

templateRead := func() string {
return fmt.Sprintf(`
data "gcore_storage_s3" "terraformtest%d_s3_data" {
name = "terraformtest%d"
}
`, random, random)
}

resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheckVars(t, GCORE_USERNAME_VAR, GCORE_PASSWORD_VAR, GCORE_STORAGE_URL_VAR)
},
CheckDestroy: func(s *terraform.State) error {
config := testAccProvider.Meta().(*Config)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

for _, rs := range s.RootModule().Resources {
if rs.Type != "gcore_storage_s3" {
continue
}
opts := []func(opt *storage.StorageListHTTPV2Params){
func(opt *storage.StorageListHTTPV2Params) { opt.Context = ctx },
func(opt *storage.StorageListHTTPV2Params) { opt.ID = &rs.Primary.ID },
}
storages, err := config.StorageClient.StoragesList(opts...)
if err != nil {
return fmt.Errorf("find storage: %w", err)
}
if len(storages) == 0 {
return nil
}
if storages[0].ProvisioningStatus == "ok" {
return fmt.Errorf("storage #%s wasn't deleted correctrly", rs.Primary.ID)
}
}
return nil
},
ProviderFactories: testAccProviders,
Steps: []resource.TestStep{
{
Config: templateCreate(),
Check: resource.ComposeTestCheckFunc(
testAccCheckResourceExists(resourceName),
resource.TestCheckResourceAttr(resourceName, StorageSchemaLocation, "s-ed1"),
),
},
{
Config: templateRead(),
Check: resource.ComposeTestCheckFunc(
testAccCheckResourceExists(dataSourceName),
resource.TestCheckResourceAttr(dataSourceName, StorageSchemaLocation, "s-ed1"),
),
},
},
})
}
Loading

0 comments on commit 2a3c71b

Please sign in to comment.