Skip to content

Commit

Permalink
Merge pull request #76 from Scalr/epic/SCALRCORE-19846
Browse files Browse the repository at this point in the history
SCALRCORE-19846 Publish Modules to preview API
  • Loading branch information
penja committed Sep 27, 2021
2 parents 3470e2a + a30a07a commit a70db63
Show file tree
Hide file tree
Showing 12 changed files with 393 additions and 20 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Expand Up @@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added
- **New data source:** `scalr_module_version` ([#76](https://github.com/Scalr/terraform-provider-scalr/pull/76))
- **New resource:** `scalr_module` ([#76](https://github.com/Scalr/terraform-provider-scalr/pull/76))

### Changed
- `scalr_workspace`: new attribute `module_version_id` ([#76](https://github.com/Scalr/terraform-provider-scalr/pull/76))

### Fixed

- `data.scalr_access_policy`: return error if access policy is not found ([#83](https://github.com/Scalr/terraform-provider-scalr/pull/83))
Expand Down
32 changes: 32 additions & 0 deletions docs/data-sources/scalr_module_version.md
@@ -0,0 +1,32 @@
---
layout: "scalr"
page_title: "Scalr: scalr_module_version"
sidebar_current: "docs-datasource-module-version-x"
description: |- Get information on the module version.
---

# scalr_module_version Data Source

This data source is used to retrieve module version data by module source and semantic version.

## Example Usage

```hcl
data "scalr_module_version" "example" {
source = "env-xxxxxx/resource-name/scalr"
version = "1.0.0"
}
```

## Argument Reference

The following arguments are supported:

* `source` - (Required) The module source.
* `version` - (Optional) The semantic version based on module version was created.

## Attribute Reference

All arguments plus:

* `id` - The identifier of а module version. Example: `modver-xxxx`
3 changes: 2 additions & 1 deletion docs/data-sources/scalr_workspace.md
Expand Up @@ -25,7 +25,6 @@ The following arguments are supported:

* `name` - (Required) Name of the workspace.
* `environment_id` - (Required) ID of the environment, in the format `env-<RANDOM STRING>`.
* `vcs_provider_id` - (Optional) ID of vcs provider, in the format `vcs-<RANDOM STRING>`.

## Attribute Reference

Expand All @@ -36,6 +35,8 @@ All arguments plus:
* `operations` - Boolean indicates if the workspace is being used for remote execution.
* `terraform_version` - The version of Terraform used for this workspace.
* `working_directory` - A relative path that Terraform will execute within.
* `module_version_id` - The identifier of a module version in the format `modver-<RANDOM STRING>`.
* `vcs_provider_id` - The identifier of a VCS provider in the format `vcs-<RANDOM STRING>`.
* `vcs_repo` - If workspace is linked to VCS repository this block shows the details, otherwise `{}`
* `created_by` - Details of the user that created the workspace.
* `has_resources` - The presence of active terraform resources in the current state version.
Expand Down
59 changes: 59 additions & 0 deletions docs/resources/scalr_module.md
@@ -0,0 +1,59 @@
---
layout: "scalr"
page_title: "Scalr: scalr_module"
sidebar_current: "docs-resource-scalr-module"
description: |-
Manages module.
---

# scalr_module Resource

Manages the state of a module in the Private Modules Registry. Create and destroy operations are available only.

## Example Usage

Basic usage:

```hcl
resource "scalr_module" "example" {
account_id = "acc-xxxxxxxxx"
environment_id = "env-xxxxxxxxx"
vcs_provider_id = "vcs-xxxxxxxxx"
vcs_repo {
identifier = "org/repo"
path = "example/terraform-<provider>-<name>"
tag_prefix = "aws/"
}
}
```

## Argument Reference
* `vcs_provider_id` - (Required) The identifier of a VCS provider in the format `vcs-<RANDOM STRING>`
* `account_id` - (Optional) The identifier of the account in the format `acc-<RANDOM STRING>`. If it is not specified the module will be registered globally and available across the whole installation.
* `environment_id` - (Optional) The identifier of an environment in the format `env-<RANDOM STRING>`. If it is not specified the module will be registered at the account level and available across all environments within the account specified in `account_id` attribute.
* `vcs_repo` - (Required) Source configuration of a VCS repository

The `vcs_repo` block supports:

* `identifier` - (Required) The identifier of a VCS repository in the format `:org/:repo` (`:org/:project/:name` is used for Azure DevOps). It refers to a organization and a repository name in a VCS provider.
* `path` - (Optional) The path to the root module folder. It Is expected to have the format '<path>/terraform-<provider_name>-<module_name>', where `<path>` stands for any folder within the repository inclusively a repository root.
* `tag_prefix` - (Optional) Registry ignores tags which do not match specified prefix, e.g. `aws/`.


## Attribute Reference

All arguments plus:

* `id` - The identifier of a module in the format `mod--<RANDOM STRING>`.
* `module_provider` - Module provider name, e.g `aws`, `azurerm`, `google`, etc.
* `name` - Name of the module, e.g. `rds`, `compute`, `kubernetes-engine`

* `source` - The source of a remote module in the private registry, e.g `env-xxxx/aws/vpc`

## Import

To import module use module ID as the import ID. For example:
```shell
terraform import scalr_module.example mod-tk4315k3lofu4i0
```
1 change: 1 addition & 0 deletions docs/resources/scalr_workspace.md
Expand Up @@ -37,6 +37,7 @@ resource "scalr_workspace" "example" {
Defaults to `true`.
* `terraform_version` - (Optional) The version of Terraform to use for this workspace. Defaults to the latest available version.
* `working_directory` - (Optional) A relative path that Terraform will be run in. Defaults to the root of the repository `""`.
* `module_version_id` - (Optional) The identifier of a module version in the format `modver-<RANDOM STRING>`. This attribute conflicts with `vcs_provider_id` and `vcs_repo` attributes.
* `vcs_provider_id` - (Optional) ID of vcs provider - required if vcs-repo present and vice versa, in the format `vcs-<RANDOM STRING>`
* `vcs_repo` - (Optional) Settings for the workspace's VCS repository.

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -7,7 +7,7 @@ require (
github.com/hashicorp/terraform v0.12.0
github.com/hashicorp/terraform-svchost v0.0.0-20191011084731-65d371908596
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/scalr/go-scalr v0.0.0-20210902095125-9ac71df3a092
github.com/scalr/go-scalr v0.0.0-20210921104644-11ed590a3eeb
)

go 1.13
6 changes: 6 additions & 0 deletions go.sum
Expand Up @@ -263,6 +263,12 @@ github.com/scalr/go-scalr v0.0.0-20210813140334-b0bce1821414 h1:F3ecDrzKMrqI701m
github.com/scalr/go-scalr v0.0.0-20210813140334-b0bce1821414/go.mod h1:n2HQ6QxqyTySiSFTOpAL18SjCKtP+xUskMygO0KuuQU=
github.com/scalr/go-scalr v0.0.0-20210902095125-9ac71df3a092 h1:KZcw4OdOFTGXcZkyd7shvLHWbtIAbH7OmiQn6+9FxnU=
github.com/scalr/go-scalr v0.0.0-20210902095125-9ac71df3a092/go.mod h1:n2HQ6QxqyTySiSFTOpAL18SjCKtP+xUskMygO0KuuQU=
github.com/scalr/go-scalr v0.0.0-20210913151726-712e82087ab0 h1:xIpvA2VJm36b/uDqqmerV2zJslox/08yPXasXDQBI6c=
github.com/scalr/go-scalr v0.0.0-20210913151726-712e82087ab0/go.mod h1:n2HQ6QxqyTySiSFTOpAL18SjCKtP+xUskMygO0KuuQU=
github.com/scalr/go-scalr v0.0.0-20210914135105-7cd19c2b6f36 h1:e8jUFEOtMKkuijUz255dMpQyQEDTSCdpcBX8cKx8xKI=
github.com/scalr/go-scalr v0.0.0-20210914135105-7cd19c2b6f36/go.mod h1:n2HQ6QxqyTySiSFTOpAL18SjCKtP+xUskMygO0KuuQU=
github.com/scalr/go-scalr v0.0.0-20210921104644-11ed590a3eeb h1:G1dbk1MikEZEqzfrbiVOxhJcS08xEQHSdiCz1h4RMaM=
github.com/scalr/go-scalr v0.0.0-20210921104644-11ed590a3eeb/go.mod h1:n2HQ6QxqyTySiSFTOpAL18SjCKtP+xUskMygO0KuuQU=
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
Expand Down
67 changes: 67 additions & 0 deletions scalr/data_source_module_version.go
@@ -0,0 +1,67 @@
package scalr

import (
"errors"
"fmt"
"log"

"github.com/hashicorp/terraform/helper/schema"
scalr "github.com/scalr/go-scalr"
)

func dataSourceModuleVersion() *schema.Resource {
return &schema.Resource{
Read: dataSourceModuleVersionRead,
Schema: map[string]*schema.Schema{
"source": {
Type: schema.TypeString,
Required: true,
},
"version": {
Type: schema.TypeString,
Optional: true,
},
"id": {
Type: schema.TypeString,
Computed: true,
},
}}
}

func dataSourceModuleVersionRead(d *schema.ResourceData, meta interface{}) error {
scalrClient := meta.(*scalr.Client)

source := d.Get("source").(string)
module, err := scalrClient.Modules.ReadBySource(ctx, source)
if err != nil {
if errors.Is(err, scalr.ErrResourceNotFound{}) {
return fmt.Errorf("Could not find module with source %s", source)
}
return fmt.Errorf("Error retrieving module: %v", err)
}
log.Printf("[DEBUG] Download module by source: %s", source)

var mv *scalr.ModuleVersion
var version string
if v, ok := d.GetOk("version"); ok {
version = v.(string)
mv, err = scalrClient.ModuleVersions.ReadBySemanticVersion(ctx, module.ID, version)
} else {
if module.LatestModuleVersion == nil {
return errors.New("The module has no version tags")
}
mv, err = scalrClient.ModuleVersions.Read(ctx, module.LatestModuleVersion.ID)
}

if err != nil {
if errors.Is(err, scalr.ErrResourceNotFound{}) {
return fmt.Errorf("Could not find module with source %s and version %s", source, version)
}
return fmt.Errorf("Error retrieving module version: %v", err)
}
log.Printf("[DEBUG] Download module version by source %s version: %s", source, version)

d.SetId(mv.ID)
d.Set("version", mv.Version)
return nil
}
8 changes: 8 additions & 0 deletions scalr/data_source_workspace.go
Expand Up @@ -28,6 +28,10 @@ func dataSourceScalrWorkspace() *schema.Resource {
Type: schema.TypeString,
Optional: true,
},
"module_version_id": {
Type: schema.TypeString,
Optional: true,
},

"auto_apply": {
Type: schema.TypeBool,
Expand Down Expand Up @@ -150,6 +154,10 @@ func dataSourceScalrWorkspaceRead(d *schema.ResourceData, meta interface{}) erro
d.Set("working_directory", workspace.WorkingDirectory)
d.Set("has_resources", workspace.HasResources)

if workspace.ModuleVersion != nil {
d.Set("module_version_id", workspace.ModuleVersion.ID)
}

if workspace.VcsProvider != nil {
d.Set("vcs_provider_id", workspace.VcsProvider.ID)
}
Expand Down
18 changes: 10 additions & 8 deletions scalr/provider.go
Expand Up @@ -63,14 +63,15 @@ func Provider() terraform.ResourceProvider {
},

DataSourcesMap: map[string]*schema.Resource{
"scalr_workspace": dataSourceScalrWorkspace(),
"scalr_workspace_ids": dataSourceScalrWorkspaceIDs(),
"scalr_current_run": dataSourceScalrCurrentRun(),
"scalr_endpoint": dataSourceScalrEndpoint(),
"scalr_webhook": dataSourceScalrWebhook(),
"scalr_environment": dataSourceScalrEnvironment(),
"scalr_role": dataSourceScalrRole(),
"scalr_access_policy": dataSourceScalrAccessPolicy(),
"scalr_workspace": dataSourceScalrWorkspace(),
"scalr_workspace_ids": dataSourceScalrWorkspaceIDs(),
"scalr_current_run": dataSourceScalrCurrentRun(),
"scalr_endpoint": dataSourceScalrEndpoint(),
"scalr_webhook": dataSourceScalrWebhook(),
"scalr_environment": dataSourceScalrEnvironment(),
"scalr_role": dataSourceScalrRole(),
"scalr_access_policy": dataSourceScalrAccessPolicy(),
"scalr_module_version": dataSourceModuleVersion(),
},

ResourcesMap: map[string]*schema.Resource{
Expand All @@ -79,6 +80,7 @@ func Provider() terraform.ResourceProvider {
"scalr_endpoint": resourceScalrEndpoint(),
"scalr_webhook": resourceScalrWebhook(),
"scalr_environment": resourceScalrEnvironment(),
"scalr_module": resourceScalrModule(),
"scalr_role": resourceScalrRole(),
"scalr_access_policy": resourceScalrAccessPolicy(),
},
Expand Down

0 comments on commit a70db63

Please sign in to comment.