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

Commit cc1ee6c

Browse files
authored
+ permanent token using for storage (#32)
* + permanent token using for storage
1 parent 2c973d0 commit cc1ee6c

File tree

9 files changed

+84
-29
lines changed

9 files changed

+84
-29
lines changed

docs/index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,5 +249,7 @@ resource "gcore_lbmember" "lbm2" {
249249
- **gcore_client_id** (String) Client id
250250
- **gcore_platform** (String) Platform ulr is used for generate jwt
251251
- **gcore_storage_api** (String) Storage API
252+
- **ignore_creds_auth_error** (Boolean) Should be set to true when you are gonna to use storage resource with permanent API-token only.
252253
- **password** (String)
254+
- **permanent_api_token** (String, Sensitive) A permanent API-token. Implemented for Storage Terraform Resource only. https://support.gcorelabs.com/hc/en-us/articles/360018625617-API-tokens
253255
- **user_name** (String)

docs/resources/gcore_storage.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,30 @@
33
page_title: "gcore_storage Resource - terraform-provider-gcorelabs"
44
subcategory: ""
55
description: |-
6-
Represent storage resource.
6+
Represent storage resource. https://storage.gcorelabs.com/storage/list
77
---
88

99
# gcore_storage (Resource)
1010

11-
Represent storage resource.
11+
Represent storage resource. https://storage.gcorelabs.com/storage/list
1212

1313
## Example Usage
1414

1515
```terraform
1616
provider gcore {
1717
user_name = "test"
1818
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
1921
gcore_platform = "https://api.gcdn.co"
2022
gcore_storage_api = "https://storage.gcorelabs.com/api"
2123
}
2224
23-
resource "gcore_storage" "tf_example_s3" {
25+
resource "gcore_storage" "tf_example_sftp" {
2426
name = "tf_example"
25-
location = "s-ed1"
26-
type = "s3"
27-
link_key_id = 199
27+
location = "mia"
28+
type = "sftp"
29+
ssh_key_id = 199 // can be used for sftp type only
2830
}
2931
```
3032

@@ -41,11 +43,13 @@ resource "gcore_storage" "tf_example_s3" {
4143

4244
- **client_id** (Number) An client id of new storage resource.
4345
- **expires** (String) A expires date of storage resource.
46+
- **generate_s3_access_key** (String) A s3 access key for new storage resource.
47+
- **generate_s3_secret_key** (String) A s3 secret key for new storage resource.
4448
- **generate_sftp_password** (Boolean) An auto generated sftp password for new storage resource.
4549
- **id** (String) The ID of this resource.
46-
- **link_key_id** (Number) An key id to link with new storage resource.
4750
- **server_alias** (String) An alias of storage resource.
4851
- **sftp_password** (String) A sftp password for new storage resource.
52+
- **ssh_key_id** (Number) An ssh key id to link with new sftp storage resource only. https://storage.gcorelabs.com/ssh-key/list
4953
- **storage_id** (Number) An id of new storage resource.
5054

5155

docs/resources/gcore_storage_key.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
page_title: "gcore_storage_key Resource - terraform-provider-gcorelabs"
44
subcategory: ""
55
description: |-
6-
Represent storage key resource.
6+
Represent storage key resource. https://storage.gcorelabs.com/ssh-key/list
77
---
88

99
# gcore_storage_key (Resource)
1010

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

1313
## Example Usage
1414

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
provider gcore {
22
user_name = "test"
33
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
46
gcore_platform = "https://api.gcdn.co"
57
gcore_storage_api = "https://storage.gcorelabs.com/api"
68
}
79

8-
resource "gcore_storage" "tf_example_s3" {
10+
resource "gcore_storage" "tf_example_sftp" {
911
name = "tf_example"
10-
location = "s-ed1"
11-
type = "s3"
12-
link_key_id = 199
12+
location = "mia"
13+
type = "sftp"
14+
ssh_key_id = 199 // can be used for sftp type only
1315
}

gcore/provider.go

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package gcore
33
import (
44
"context"
55
"fmt"
6+
"log"
67
"net/http"
78

89
storageSDK "github.com/G-Core/gcorelabs-storage-sdk-go"
@@ -14,6 +15,11 @@ import (
1415
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1516
)
1617

18+
const (
19+
ProviderOptPermanentToken = "permanent_api_token"
20+
ProviderOptSkipCredsAuthErr = "ignore_creds_auth_error"
21+
)
22+
1723
func Provider() *schema.Provider {
1824
return &schema.Provider{
1925
Schema: map[string]*schema.Schema{
@@ -27,6 +33,19 @@ func Provider() *schema.Provider {
2733
Optional: true,
2834
DefaultFunc: schema.EnvDefaultFunc("GCORE_PASSWORD", ""),
2935
},
36+
ProviderOptPermanentToken: {
37+
Type: schema.TypeString,
38+
Optional: true,
39+
Sensitive: true,
40+
Description: "A permanent API-token. Implemented for Storage Terraform Resource only. https://support.gcorelabs.com/hc/en-us/articles/360018625617-API-tokens",
41+
DefaultFunc: schema.EnvDefaultFunc("GCORE_PERMANENT_TOKEN", ""),
42+
},
43+
ProviderOptSkipCredsAuthErr: {
44+
Type: schema.TypeBool,
45+
Optional: true,
46+
Description: "Should be set to true when you are gonna to use storage resource with permanent API-token only.",
47+
DefaultFunc: schema.EnvDefaultFunc("GCORE_PERMANENT_TOKEN", ""),
48+
},
3049
"gcore_platform": &schema.Schema{
3150
Type: schema.TypeString,
3251
Optional: true,
@@ -106,6 +125,7 @@ func Provider() *schema.Provider {
106125
func providerConfigure(ctx context.Context, d *schema.ResourceData) (interface{}, diag.Diagnostics) {
107126
username := d.Get("user_name").(string)
108127
password := d.Get("password").(string)
128+
permanentToken := d.Get(ProviderOptPermanentToken).(string)
109129
api := d.Get("gcore_api").(string)
110130
cdnAPI := d.Get("gcore_cdn_api").(string)
111131
storageAPI := d.Get("gcore_storage_api").(string)
@@ -122,8 +142,15 @@ func providerConfigure(ctx context.Context, d *schema.ResourceData) (interface{}
122142
AllowReauth: true,
123143
ClientID: clientID,
124144
})
145+
146+
skipAuthErr, ok := d.GetOk(ProviderOptSkipCredsAuthErr)
147+
if err != nil && !(ok == true || skipAuthErr.(bool) == true) {
148+
return nil, diag.FromErr(fmt.Errorf("init auth client: %w", err))
149+
150+
}
125151
if err != nil {
126-
return nil, diag.FromErr(err)
152+
provider = &gcorecloud.ProviderClient{}
153+
log.Printf("[WARN] init auth client: %s\n", err)
127154
}
128155

129156
cdnProvider := gcdnProvider.NewClient(cdnAPI, gcdnProvider.WithSignerFunc(func(req *http.Request) error {
@@ -138,9 +165,11 @@ func providerConfigure(ctx context.Context, d *schema.ResourceData) (interface{}
138165
}
139166

140167
config := Config{
141-
Provider: provider,
142-
CDNClient: cdnService,
143-
StorageClient: storageSDK.NewSDK(stHost, stPath, storageSDK.WithBearerAuth(provider.AccessToken)),
168+
Provider: provider,
169+
CDNClient: cdnService,
170+
StorageClient: storageSDK.NewSDK(stHost, stPath,
171+
storageSDK.WithBearerAuth(provider.AccessToken),
172+
storageSDK.WithPermanentTokenAuth(func() string { return permanentToken })),
144173
}
145174

146175
return &config, diags

gcore/resource_gcore_storage.go

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@ import (
1515

1616
const (
1717
StorageSchemaGenerateSftpPassword = "generate_sftp_password"
18+
StorageSchemaGenerateS3AccessKey = "generate_s3_access_key"
19+
StorageSchemaGenerateS3SecretKey = "generate_s3_secret_key"
1820
StorageSchemaLocation = "location"
1921
StorageSchemaName = "name"
2022
StorageSchemaType = "type"
2123
StorageSchemaId = "storage_id"
2224
StorageSchemaClientId = "client_id"
2325
StorageSchemaSftpPassword = "sftp_password"
24-
StorageSchemaKeyId = "link_key_id"
26+
StorageSchemaKeyId = "ssh_key_id"
2527
StorageSchemaExpires = "expires"
2628
StorageSchemaServerAlias = "server_alias"
2729
)
@@ -94,6 +96,18 @@ func resourceStorage() *schema.Resource {
9496
Optional: true,
9597
Description: "A sftp password for new storage resource.",
9698
},
99+
StorageSchemaGenerateS3AccessKey: {
100+
Type: schema.TypeString,
101+
Optional: true,
102+
Computed: true,
103+
Description: "A s3 access key for new storage resource.",
104+
},
105+
StorageSchemaGenerateS3SecretKey: {
106+
Type: schema.TypeString,
107+
Optional: true,
108+
Computed: true,
109+
Description: "A s3 secret key for new storage resource.",
110+
},
97111
StorageSchemaGenerateSftpPassword: {
98112
Type: schema.TypeBool,
99113
Optional: true,
@@ -102,14 +116,14 @@ func resourceStorage() *schema.Resource {
102116
StorageSchemaKeyId: {
103117
Type: schema.TypeInt,
104118
Optional: true,
105-
Description: "An key id to link with new storage resource.",
119+
Description: "An ssh key id to link with new sftp storage resource only. https://storage.gcorelabs.com/ssh-key/list",
106120
},
107121
},
108122
CreateContext: resourceStorageCreate,
109123
ReadContext: resourceStorageRead,
110124
UpdateContext: resourceStorageUpdate,
111125
DeleteContext: resourceStorageDelete,
112-
Description: "Represent storage resource.",
126+
Description: "Represent storage resource. https://storage.gcorelabs.com/storage/list",
113127
}
114128
}
115129

@@ -151,6 +165,15 @@ func resourceStorageCreate(ctx context.Context, d *schema.ResourceData, m interf
151165
dErr = resourceStorageRead(ctx, d, m)
152166
}()
153167
*id = int(result.ID)
168+
if result.Credentials.SftpPassword != "" {
169+
_ = d.Set(StorageSchemaSftpPassword, result.Credentials.SftpPassword)
170+
}
171+
if result.Credentials.S3.AccessKey != "" {
172+
_ = d.Set(StorageSchemaGenerateS3AccessKey, result.Credentials.S3.AccessKey)
173+
}
174+
if result.Credentials.S3.SecretKey != "" {
175+
_ = d.Set(StorageSchemaGenerateS3SecretKey, result.Credentials.S3.SecretKey)
176+
}
154177

155178
keyId := d.Get(StorageSchemaKeyId).(int)
156179
if keyId == 0 {
@@ -192,10 +215,6 @@ func resourceStorageRead(ctx context.Context, d *schema.ResourceData, m interfac
192215
}
193216
st := result[0]
194217

195-
sftpPass := ""
196-
if st.Credentials != nil {
197-
sftpPass = st.Credentials.SftpPassword
198-
}
199218
nameParts := strings.Split(st.Name, "-")
200219
if len(nameParts) > 1 {
201220
clientID, _ := strconv.ParseInt(nameParts[0], 10, 64)
@@ -209,7 +228,6 @@ func resourceStorageRead(ctx context.Context, d *schema.ResourceData, m interfac
209228
_ = d.Set(StorageSchemaId, st.ID)
210229
_ = d.Set(StorageSchemaType, st.Type)
211230
_ = d.Set(StorageSchemaLocation, st.Location)
212-
_ = d.Set(StorageSchemaSftpPassword, sftpPass)
213231

214232
return nil
215233
}

gcore/resource_gcore_storage_key.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func resourceStorageKey() *schema.Resource {
4444
CreateContext: resourceStorageKeyCreate,
4545
ReadContext: resourceStorageKeyRead,
4646
DeleteContext: resourceStorageKeyDelete,
47-
Description: "Represent storage key resource.",
47+
Description: "Represent storage key resource. https://storage.gcorelabs.com/ssh-key/list",
4848
}
4949
}
5050

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/terraform-providers/terraform-provider-gcorelabs
33
go 1.14
44

55
require (
6-
github.com/G-Core/gcorelabs-storage-sdk-go v0.0.7
6+
github.com/G-Core/gcorelabs-storage-sdk-go v0.0.8
77
github.com/G-Core/gcorelabscdn-go v0.0.0-20210503173228-b4ac8b2402ff
88
github.com/G-Core/gcorelabscloud-go v0.4.6
99
github.com/google/uuid v1.1.2 // indirect

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ github.com/Azure/go-autorest/logger v0.1.0/go.mod h1:oExouG+K6PryycPJfVSxi/koC6L
4141
github.com/Azure/go-autorest/tracing v0.5.0/go.mod h1:r/s2XiOKccPW3HrqB+W0TQzfbtp2fGCgRFtBroKn4Dk=
4242
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
4343
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
44-
github.com/G-Core/gcorelabs-storage-sdk-go v0.0.7 h1:HeKhqd9PvSHX2BMWPkKb8QxtHeitVZxfG6jLxUAHmcM=
45-
github.com/G-Core/gcorelabs-storage-sdk-go v0.0.7/go.mod h1:BZef79y4G28n8ic3x6iQWbW+mtpHPSUyJRfIRSkeAJw=
44+
github.com/G-Core/gcorelabs-storage-sdk-go v0.0.8 h1:+HF1Jv8vR8LSJ0VYiJBeYXj1Qu4d85BoizLLMReq9zk=
45+
github.com/G-Core/gcorelabs-storage-sdk-go v0.0.8/go.mod h1:BZef79y4G28n8ic3x6iQWbW+mtpHPSUyJRfIRSkeAJw=
4646
github.com/G-Core/gcorelabscdn-go v0.0.0-20210503173228-b4ac8b2402ff h1:kIH66Shwb0Y9kvBgykpzmQn2soiHDTCJ/Rr5cQQ1cOk=
4747
github.com/G-Core/gcorelabscdn-go v0.0.0-20210503173228-b4ac8b2402ff/go.mod h1:iSGXaTvZBzDHQW+rKFS918BgFVpONcyLEijwh8WsXpE=
4848
github.com/G-Core/gcorelabscloud-go v0.4.6 h1:+pNeTKWuhR52Qavnzt+r6rCDJWumymkJTjKQV4+Tl5Y=

0 commit comments

Comments
 (0)