Skip to content

Commit

Permalink
feat: object storage management (#676)
Browse files Browse the repository at this point in the history
* feat: added accessKeys resource

* fixed some plugin framework compatibilities

* feat: added data sources, some refactoring

* fix: working data sources, use valueString and other fixes

* fix: getting version and terraform version like in the old provider

* fix: rename bucket references

* doc: added docs

* test: added tests wip, renamed files

* test: fix tests

* fix: get specific client from bundle, added some checks to tests

* fix: check for err and return if not nil to reduce complexity

* doc: added comments

* fix: made s3 work with sdk bundle

* refactor: move cloudapi client initialization in method, sonar fixes

* doc: added comments

* sonar fixes

* added the sdk to run linter

* fix: linter issues

* fix: linter issues 2

* fix: try fix imports

* fix: try fix imports 2

* chore: added sdk from repo, some renaming changes

* chore: rename to object-storage-management

* chore: pr review changes and linter

* chore: linter error

* chore: undo linter changes

* fix: make description computed

* feat: added option to get access key using accesskeyid or description

* doc: added changelog, summary, use sdk from github

* fix: added check for when no identifier is set or calls fail, linter errors
  • Loading branch information
rmocanu-ionos authored Nov 26, 2024
1 parent 5307ac5 commit 3655af1
Show file tree
Hide file tree
Showing 81 changed files with 14,421 additions and 64 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
## 6.6.4 - upcoming release
### New Product - **Object Storage Management**:
- `Resources`:
- [ionoscloud_object_storage_accesskey](docs/resources/object_storage_accesskey.md)
- `Data Sources`:
- [ionoscloud_object_storage_accesskey](docs/data-sources/object_storage_accesskey.md)
- [ionoscloud_object_storage_region](docs/data-sources/object_storage_region.md)
### Fixes
- Refactor `ionoscloud_share` and `ionoscloud_nic` data sources
- Remove sleep and delete from `ionoscloud_share` resource
Expand Down
37 changes: 37 additions & 0 deletions docs/data-sources/object_storage_accesskey.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
subcategory: "Object storage management"
layout: "ionoscloud"
page_title: "IonosCloud : object_storage_accesskey"
sidebar_current: "docs-datasource-object_storage_accesskey"
description: |-
Get information on a IonosCloud Object storage Accesskey
---

# ionoscloud_object_storage_accesskey

The **Object Storage Accesskey data source** can be used to search for and return an existing Object Storage Accesskeys.

## Example Usage

### By ID
```hcl
data "ionoscloud_object_storage_accesskey" "example" {
id = "accesskey_id"
}
```

## Argument Reference

* `id` - (Optional) Id of an existing object storage accesskey that you want to search for.
* `accesskey` - (Optional) Access key metadata is a string of 92 characters.
* `description` - (Optional) Description of the Access key.

## Attributes Reference

The following attributes are returned by the datasource:

- `id` - The ID (UUID) of the AccessKey.
- `description` - Description of the Access key.
- `accesskey` - Access key metadata is a string of 92 characters.
- `canonical_user_id` - The canonical user ID which is valid for user-owned buckets.
- `contract_user_id` - The contract user ID which is valid for contract-owned buckets
39 changes: 39 additions & 0 deletions docs/data-sources/object_storage_region.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
subcategory: "Object storage management"
layout: "ionoscloud"
page_title: "IonosCloud : object_storage_region"
sidebar_current: "docs-datasource-object_storage_region"
description: |-
Get information on a IonosCloud Object Storage Region
---

# ionoscloud_object_storage_region

The **Object storage region data source** can be used to search for and return an existing S3 Regions.

## Example Usage

### By ID
```hcl
data "ionoscloud_object_storage_region" "example" {
id = "region_id"
}
```

## Argument Reference

* `id` - (Required) Id of an existing object storage Region that you want to search for.

## Attributes Reference

The following attributes are returned by the datasource:

- `id` - The id of the region
- `version` - The version of the region properties
- `endpoint` - The endpoint URL for the region
- `website` - The website URL for the region
- `storage_classes` - The available classes in the region
- `location` - The data center location of the region as per [Get Location](/docs/cloud/v6/#tag/Locations/operation/locationsGet). *Can't be used as `LocationConstraint` on bucket creation.*
- `capability` - The capabilities of the region
* `iam` - Indicates if IAM policy based access is supported
* `s3select` - Indicates if S3 Select is supported
44 changes: 44 additions & 0 deletions docs/resources/object_storage_accesskey.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
subcategory: "Object storage management"
layout: "ionoscloud"
page_title: "IonosCloud: object_storage_accesskey"
sidebar_current: "docs-resource-object_storage_accesskey"
description: |-
Creates and manages IonosCloud Object Storage Accesskeys.
---

# ionoscloud_object_storage_accesskey

Manages an **Object Storage Accesskey** on IonosCloud.

## Example Usage

```hcl
resource "ionoscloud_object_storage_accesskey" "example" {
description = "my description"
}
```

## Argument Reference

The following arguments are supported:

- `description` - (Optional)[string] Description of the Access key.
- `id` - (Computed) The ID (UUID) of the AccessKey.
- `accesskey` - (Computed) Access key metadata is a string of 92 characters.
- `secretkey` - (Computed) The secret key of the Access key.
- `canonical_user_id` - (Computed) The canonical user ID which is valid for user-owned buckets.
- `contract_user_id` - (Computed) The contract user ID which is valid for contract-owned buckets
- `timeouts` - (Optional) Timeouts for this resource.
- `create` - (Optional)[string] Time to wait for the bucket to be created. Default is `10m`.
- `delete` - (Optional)[string] Time to wait for the bucket to be deleted. Default is `10m`.

## Import

An object storage accesskey resource can be imported using its `resource id`, e.g.

```shell
terraform import ionoscloud_object_storage_accesskey.demo {objectStorageAccesskeyId}
```

This can be helpful when you want to import Object Storage Accesskeys which you have already created manually or using other means, outside of terraform.
7 changes: 7 additions & 0 deletions gitbook_docs/summary.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,13 @@
* [Bucket Policy](../docs/data-sources/s3_bucket_policy.md)
* [Object](../docs/data-sources/s3_object.md)
* [Objects](../docs/data-sources/s3_objects.md)

* Object Storage Management
* Resources
* [Access Key](../docs/resources/object_storage_accesskey.md)
* Data Sources
* [Access Key](../docs/data-sources/object_storage_accesskey.md)
* [Region](../docs/data-sources/object_storage_region.md)

* CDN
* Resources
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ require (
github.com/ionos-cloud/sdk-go-kafka v1.0.0
github.com/ionos-cloud/sdk-go-nfs v1.0.0
github.com/ionos-cloud/sdk-go-object-storage v1.1.0
github.com/ionos-cloud/sdk-go-object-storage-management v1.0.0
github.com/ionos-cloud/sdk-go-vm-autoscaling v1.0.1
github.com/ionos-cloud/sdk-go/v6 v6.3.0
github.com/mitchellh/go-homedir v1.1.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ github.com/ionos-cloud/sdk-go-nfs v1.0.0 h1:CyiemTDPg2jAjtTT4JHzvOpjq6lqe7YbXx2i
github.com/ionos-cloud/sdk-go-nfs v1.0.0/go.mod h1:ffyMIPVknZ7dpi/+RomWXNNYV7ZjtK8KYSsGjVlbqgA=
github.com/ionos-cloud/sdk-go-object-storage v1.1.0 h1:4JTsxrbzFltF9liAHZ2I9f53rpkrXm8642UwQoQ+pMc=
github.com/ionos-cloud/sdk-go-object-storage v1.1.0/go.mod h1:GMNkyZp5B70QInLTS79LWbZ7hoEy4F8TWL82zeInwBo=
github.com/ionos-cloud/sdk-go-object-storage-management v1.0.0 h1:pHRKSNT+cCcr5ghWMkCgY2ErxrcQCAjlCJu/VrdexUw=
github.com/ionos-cloud/sdk-go-object-storage-management v1.0.0/go.mod h1:BnEapZcQp6FEirEin6pY9NICXD1mCrI4sJ+wOSKpyCY=
github.com/ionos-cloud/sdk-go-vm-autoscaling v1.0.1 h1:KABL25MC7DrIHn9lQzKSPkwXhqvRkPYtFd+1HEogmAE=
github.com/ionos-cloud/sdk-go-vm-autoscaling v1.0.1/go.mod h1:Q5d1R6silarsX5jWLPBHd/1PSC5zZNf2ONvXB+fygC0=
github.com/ionos-cloud/sdk-go/v6 v6.3.0 h1:/lTieTH9Mo/CWm3cTlFLnK10jgxjUGkAqRffGqvPteY=
Expand Down
75 changes: 73 additions & 2 deletions internal/framework/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package provider

import (
"context"
"fmt"
"log"
"net/http"
"os"
"runtime"
"strconv"

"github.com/hashicorp/terraform-plugin-framework/datasource"
Expand All @@ -12,9 +15,31 @@ import (
"github.com/hashicorp/terraform-plugin-framework/provider/schema"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-sdk/v2/meta"

ionoscloud "github.com/ionos-cloud/sdk-go/v6"

"github.com/ionos-cloud/terraform-provider-ionoscloud/v6/internal/framework/services/objectstorage"
objstorage "github.com/ionos-cloud/terraform-provider-ionoscloud/v6/services/objectstorage"
"github.com/ionos-cloud/terraform-provider-ionoscloud/v6/internal/framework/services/objectstoragemanagement"
"github.com/ionos-cloud/terraform-provider-ionoscloud/v6/services"
apiGatewayService "github.com/ionos-cloud/terraform-provider-ionoscloud/v6/services/apigateway"
autoscalingService "github.com/ionos-cloud/terraform-provider-ionoscloud/v6/services/autoscaling"
cdnService "github.com/ionos-cloud/terraform-provider-ionoscloud/v6/services/cdn"
"github.com/ionos-cloud/terraform-provider-ionoscloud/v6/services/cert"
crService "github.com/ionos-cloud/terraform-provider-ionoscloud/v6/services/containerregistry"
dataplatformService "github.com/ionos-cloud/terraform-provider-ionoscloud/v6/services/dataplatform"
dbaasService "github.com/ionos-cloud/terraform-provider-ionoscloud/v6/services/dbaas"
"github.com/ionos-cloud/terraform-provider-ionoscloud/v6/services/dbaas/inmemorydb"
"github.com/ionos-cloud/terraform-provider-ionoscloud/v6/services/dbaas/mariadb"
dnsService "github.com/ionos-cloud/terraform-provider-ionoscloud/v6/services/dns"
kafkaService "github.com/ionos-cloud/terraform-provider-ionoscloud/v6/services/kafka"
loggingService "github.com/ionos-cloud/terraform-provider-ionoscloud/v6/services/logging"
nfsService "github.com/ionos-cloud/terraform-provider-ionoscloud/v6/services/nfs"
objectStorageService "github.com/ionos-cloud/terraform-provider-ionoscloud/v6/services/objectstorage"
objectStorageManagementService "github.com/ionos-cloud/terraform-provider-ionoscloud/v6/services/objectstoragemanagement"
"github.com/ionos-cloud/terraform-provider-ionoscloud/v6/services/vpn"
"github.com/ionos-cloud/terraform-provider-ionoscloud/v6/utils"
"github.com/ionos-cloud/terraform-provider-ionoscloud/v6/utils/constant"
)

// ClientOptions is the configuration for the provider.
Expand Down Expand Up @@ -128,6 +153,9 @@ func (p *IonosCloudProvider) Configure(ctx context.Context, req provider.Configu
secretKey := os.Getenv("IONOS_S3_SECRET_KEY")
region := os.Getenv("IONOS_S3_REGION")
endpoint := os.Getenv("IONOS_API_URL")
terraformVersion := req.TerraformVersion
version := ionoscloud.Version

insecureStr := os.Getenv("IONOS_ALLOW_INSECURE")
insecureBool := false
if insecureStr != "" {
Expand Down Expand Up @@ -176,15 +204,55 @@ func (p *IonosCloudProvider) Configure(ctx context.Context, req provider.Configu
return
}

cleanedEndpoint := utils.CleanURL(endpoint)

if insecureBool == true {
resp.Diagnostics.AddWarning("insecure mode enabled", "This is not recommended for production environments.")
}

client := objstorage.NewClient(accessKey, secretKey, region, endpoint, insecureBool)
client := &services.SdkBundle{
CDNClient: cdnService.NewCDNClient(username, password, token, endpoint, version, terraformVersion, insecureBool),
AutoscalingClient: autoscalingService.NewClient(username, password, token, cleanedEndpoint, version, terraformVersion, insecureBool),
CertManagerClient: cert.NewClient(username, password, token, cleanedEndpoint, version, terraformVersion, insecureBool),
CloudApiClient: newCloudapiClient(username, password, token, endpoint, "DEV", terraformVersion, insecureBool),
ContainerClient: crService.NewClient(username, password, token, cleanedEndpoint, version, terraformVersion, insecureBool),
DataplatformClient: dataplatformService.NewClient(username, password, token, cleanedEndpoint, version, terraformVersion, insecureBool),
DNSClient: dnsService.NewClient(username, password, token, cleanedEndpoint, version, terraformVersion, insecureBool),
LoggingClient: loggingService.NewClient(username, password, token, cleanedEndpoint, terraformVersion, insecureBool),
MariaDBClient: mariadb.NewMariaDBClient(username, password, token, cleanedEndpoint, version, terraformVersion, insecureBool),
MongoClient: dbaasService.NewMongoClient(username, password, token, cleanedEndpoint, version, terraformVersion, insecureBool),
NFSClient: nfsService.NewClient(username, password, token, cleanedEndpoint, version, terraformVersion, insecureBool),
PsqlClient: dbaasService.NewPsqlClient(username, password, token, cleanedEndpoint, version, terraformVersion, insecureBool),
KafkaClient: kafkaService.NewClient(username, password, token, cleanedEndpoint, version, terraformVersion, insecureBool),
APIGatewayClient: apiGatewayService.NewClient(
username, password, token, cleanedEndpoint, version, terraformVersion, insecureBool,
),
VPNClient: vpn.NewClient(username, password, token, cleanedEndpoint, terraformVersion, insecureBool),
InMemoryDBClient: inmemorydb.NewInMemoryDBClient(username, password, token, cleanedEndpoint, version, terraformVersion, insecureBool),
S3Client: objectStorageService.NewClient(accessKey, secretKey, region, endpoint, insecureBool),
ObjectStorageManagementClient: objectStorageManagementService.NewClient(username, password, token, cleanedEndpoint, version, terraformVersion, insecureBool),
}

resp.DataSourceData = client
resp.ResourceData = client
}

func newCloudapiClient(username, password, token, endpoint, version, terraformVersion string, insecure bool) *ionoscloud.APIClient {
newConfig := ionoscloud.NewConfiguration(username, password, token, endpoint)
newConfig.UserAgent = fmt.Sprintf(
"terraform-provider/%s_ionos-cloud-sdk-go/%s_hashicorp-terraform/%s_terraform-plugin-sdk/%s_os/%s_arch/%s",
version, ionoscloud.Version, terraformVersion, meta.SDKVersionString(), runtime.GOOS, runtime.GOARCH, //nolint:staticcheck
)
if os.Getenv(constant.IonosDebug) != "" {
newConfig.Debug = true
}
newConfig.MaxRetries = constant.MaxRetries
newConfig.WaitTime = constant.MaxWaitTime
newConfig.HTTPClient = &http.Client{Transport: utils.CreateTransport(insecure)}
client := ionoscloud.NewAPIClient(newConfig)
return client
}

// Resources returns the resources for the provider.
func (p *IonosCloudProvider) Resources(_ context.Context) []func() resource.Resource {
return []func() resource.Resource{
Expand All @@ -199,6 +267,7 @@ func (p *IonosCloudProvider) Resources(_ context.Context) []func() resource.Reso
objectstorage.NewBucketCorsConfigurationResource,
objectstorage.NewBucketLifecycleConfigurationResource,
objectstorage.NewBucketWebsiteConfigurationResource,
objectstoragemanagement.NewAccesskeyResource,
}
}

Expand All @@ -209,5 +278,7 @@ func (p *IonosCloudProvider) DataSources(_ context.Context) []func() datasource.
objectstorage.NewObjectDataSource,
objectstorage.NewBucketPolicyDataSource,
objectstorage.NewObjectsDataSource,
objectstoragemanagement.NewRegionDataSource,
objectstoragemanagement.NewAccesskeyDataSource,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"

"github.com/ionos-cloud/terraform-provider-ionoscloud/v6/services"
"github.com/ionos-cloud/terraform-provider-ionoscloud/v6/services/objectstorage"

"github.com/hashicorp/terraform-plugin-framework/datasource"
Expand Down Expand Up @@ -34,17 +35,17 @@ func (d *bucketDataSource) Configure(ctx context.Context, req datasource.Configu
return
}

client, ok := req.ProviderData.(*objectstorage.Client)
clientBundle, ok := req.ProviderData.(*services.SdkBundle)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected *objectstorage.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
fmt.Sprintf("Expected *services.SdkBundle, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)

return
}

d.client = client
d.client = clientBundle.S3Client
}

// Schema returns the schema for the data source.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"

"github.com/ionos-cloud/terraform-provider-ionoscloud/v6/services"
"github.com/ionos-cloud/terraform-provider-ionoscloud/v6/services/objectstorage"

"github.com/hashicorp/terraform-plugin-framework-jsontypes/jsontypes"
Expand Down Expand Up @@ -35,17 +36,17 @@ func (d *bucketPolicyDataSource) Configure(ctx context.Context, req datasource.C
return
}

client, ok := req.ProviderData.(*objectstorage.Client)
clientBundle, ok := req.ProviderData.(*services.SdkBundle)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected *objectstorage.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
fmt.Sprintf("Expected *services.SdkBundle, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)

return
}

d.client = client
d.client = clientBundle.S3Client
}

// Schema returns the schema for the data source.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"

"github.com/ionos-cloud/terraform-provider-ionoscloud/v6/services"
"github.com/ionos-cloud/terraform-provider-ionoscloud/v6/services/objectstorage"
)

Expand Down Expand Up @@ -39,17 +40,17 @@ func (d *objectDataSource) Configure(ctx context.Context, req datasource.Configu
return
}

client, ok := req.ProviderData.(*objectstorage.Client)
clientBundle, ok := req.ProviderData.(*services.SdkBundle)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected *objectstorage.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
fmt.Sprintf("Expected *services.SdkBundle, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)

return
}

d.client = client
d.client = clientBundle.S3Client
}

// Schema returns the schema for the object data source.
Expand Down
Loading

0 comments on commit 3655af1

Please sign in to comment.