Skip to content

Commit

Permalink
Overhaul (#57)
Browse files Browse the repository at this point in the history
* Import kypo library from separate repository

* Sort imports

* Pass context structure to kypo client library

* Add timeouts to create sandbox_allocation_unit_resource

* Generate documentation

* Implement timeouts for sandbox_allocation_unit_resource read, update and delete

* Update documentation

* Generate documentation

* Restore docs/guides on go generate

* Update timeouts default to no timeout

* Update error handling from kypo client

* Extract allocation request list plan modifier

* Add poll times to sandbox allocation unit

* Generate documentation

* Handle ErrNotFound properly in Delete methods

* Move provider tests to a separate package

* Update README

* Delete unused CHANGELOG

* Add License information

* Add retry_count to provider configuration

* Generate documentation

* Bump dependencies

* Upgrade to Go 1.21
  • Loading branch information
vydrazde authored Dec 17, 2023
1 parent 5dd1a49 commit 202c269
Show file tree
Hide file tree
Showing 42 changed files with 752 additions and 1,849 deletions.
3 changes: 3 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: MPL-2.0

# Visit https://golangci-lint.run/ for usage documentation
# and information on other useful linters
issues:
Expand Down
3 changes: 3 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: MPL-2.0

# Visit https://goreleaser.com for documentation on how to customize this
# behavior.
before:
Expand Down
3 changes: 0 additions & 3 deletions CHANGELOG.md

This file was deleted.

396 changes: 21 additions & 375 deletions LICENSE

Large diffs are not rendered by default.

109 changes: 36 additions & 73 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,88 +1,51 @@
# Terraform KYPO Provider

This [KYPO Provider]() allows [Terraform](https://www.terraform.io/) to manage [KYPO CRP](https://crp.kypo.muni.cz/) resources.
Terraform provider for KYPO allows [Terraform](https://www.terraform.io/) to manage [KYPO CRP](https://crp.kypo.muni.cz/) resources.

**This Terraform provider is in early development and not intended for use yet. Documentation is missing and breaking changes will occur.**
See documentation at the [Terraform Registry](https://registry.terraform.io/providers/vydrazde/kypo/latest/docs).

## Example Usage

## Requirements

- [Terraform](https://www.terraform.io/downloads.html) >= 1.0
- [Go](https://golang.org/doc/install) >= 1.18

## Building The Provider

1. Clone the repository
2. Enter the repository directory
3. Install dependencies using the Go `mod tidy` command:
```shell
go mod tidy
```

4. Build the provider using the Go `install` command:

```shell
go install
```

## Using the provider

1. Find the `GOBIN` path where Go installs your binaries. Your path may vary depending on how your Go environment variables are configured.
```shell
go env GOBIN
```
If the `GOBIN` go environment variable is not set, use the default path, `/home/<Username>/go/bin`.

2. Create a new file called `.terraformrc` in the root directory (`~`), then add the `dev_overrides` block below.
Change the `<PATH>` to the value returned from the `echo $GOBIN` command above.
```
provider_installation {
dev_overrides {
"registry.terraform.io/vydrazde/kypo" = "<PATH>"
```terraform
terraform {
required_providers {
kypo = {
source = "vydrazde/kypo"
version = "0.3.1"
}
}
# For all other providers, install them directly from their origin provider
# registries as normal. If you omit this, Terraform will _only_ use
# the dev_overrides block, and so no other providers will be available.
direct {}
}
```
3. Set up your provider configuration. You can either copy the `examples/provider/provider.tf` file to a folder of one of the
other `examples` and modify it, or fill the environment variables `KYPO_ENDPOINT`, `KYPO_CLIENT_ID`, `KYPO_USERNAME` and `KYPO_PASSWORD`.

4. Now you can use one of the examples in the `examples` directory and run
```shell
terraform plan
```

## Adding Dependencies
provider "kypo" {
endpoint = "https://your.kypo.ex" # Or use KYPO_ENDPOINT env var
client_id = "***" # Or use KYPO_CLIENT_ID env var
username = "user" # Or use KYPO_USERNAME env var
password = "***" # Or use KYPO_PASSWORD env var
}
This provider uses [Go modules](https://github.com/golang/go/wiki/Modules).
Please see the Go documentation for the most up-to-date information about using Go modules.
resource "kypo_sandbox_definition" "example" {
url = "[email protected]:muni-kypo-trainings/games/junior-hacker.git"
rev = "master"
}
To add a new dependency `github.com/author/dependency` to your Terraform provider:
resource "kypo_sandbox_pool" "example" {
definition = {
id = kypo_sandbox_definition.example.id
}
max_size = 1
}
```shell
go get github.com/author/dependency
go mod tidy
resource "kypo_sandbox_allocation_unit" "example" {
pool_id = kypo_sandbox_pool.example.id
}
```

Then commit the changes to `go.mod` and `go.sum`.

## Developing the Provider

If you wish to work on the provider, you'll first need [Go](http://www.golang.org) installed on your machine (see [Requirements](#requirements) above).

To compile the provider, run `go install`. This will build the provider and put the provider binary in the `$GOPATH/bin` directory.
# License
Parts of the Source Code, namely [GNUmakefile](./GNUmakefile), [.golangci.yml](./.golangci.yml), [.goreleaser.yml](./.goreleaser.yml),
[tools/tools.go](./tools/tools.go),
[internal/validators/timeduration.go](./internal/validators/timeduration.go) and [internal/validators/timeduration_test.go](./internal/validators/timeduration_test.go),
is subject to the terms of the Mozilla Public License, v. 2.0. You can obtain the license at https://mozilla.org/MPL/2.0/.
The Source Code was used from [HashiCorp terraform-provider-scaffolding-framework](https://github.com/hashicorp/terraform-provider-scaffolding-framework) and [HashiCorp terraform-plugin-framework-timeouts](https://github.com/hashicorp/terraform-plugin-framework-timeouts) repositories.

To generate or update documentation, run `go generate`.
The rest of the Source Code is subject to the [MIT License](./LICENSE).

In order to run the full suite of Acceptance tests, run `make testacc`.

*Note:* Acceptance tests create real resources, and often cost money to run.

```shell
make testacc
```
10 changes: 5 additions & 5 deletions docs/data-sources/sandbox_request_output.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
page_title: "kypo_sandbox_request_output Data Source - terraform-provider-kypo"
subcategory: ""
description: |-
Sandbox allocation request output of one of three stages. Terraform, Networking Ansible or User Ansible.
Sandbox allocation request output of one of three stages, which are terraform, networking-ansible or user-ansible.
---

# kypo_sandbox_request_output (Data Source)

Sandbox allocation request output of one of three stages. Terraform, Networking Ansible or User Ansible.
Sandbox allocation request output of one of three stages, which are terraform, networking-ansible or user-ansible.



Expand All @@ -17,12 +17,12 @@ Sandbox allocation request output of one of three stages. Terraform, Networking

### Required

- `id` (Number) Sandbox Allocation Request Id
- `id` (Number) Id of the sandbox allocation request to read the output from. The sandbox allocation request is always the same as sandbox allocation unit id

### Optional

- `stage` (String) Sandbox Request stage to get the output of. Must be one of `user-ansible`, `networking-ansible` or `terraform`. Defaults to `user-ansible`
- `stage` (String) Sandbox request stage to get the output of. Must be one of `user-ansible`, `networking-ansible` or `terraform`. Defaults to `user-ansible`

### Read-Only

- `result` (String) Output of stage
- `result` (String) The resulting output of the stage, concatenated into a single string
12 changes: 12 additions & 0 deletions docs/guides/getting_oidc_client_id.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
page_title: "How to get client_id of KYPO CRP"
---
[client_id](https://registry.terraform.io/providers/vydrazde/kypo/latest/docs#client_id) is one of the parameters for using the Terraform KYPO provider. As of KYPO version `23.12`, the default value `KYPO-Client` should work, for older KYPO instances or cases where the `client_id` has been changed follow this guide.

To get the `client_id` value for your KYPO CRP instance, visit the homepage of the KYPO instance in an anonymous window and open browser developer tools.

![image](https://github.com/vydrazde/terraform-provider-kypo/assets/80331839/60c9f152-e1c7-49e9-a386-80634b1f633a)

Click the `Login with local issuer` or `Login with local Keycloak` button and see one of the first network requests, where you will see the `client_id` among request headers.

![screenshot](https://github.com/vydrazde/terraform-provider-kypo/assets/80331839/a6a015d4-1e25-4aaa-895f-e265a171732f)
3 changes: 2 additions & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ provider "kypo" {

### Optional

- `client_id` (String) KYPO local OIDC client ID. Will be ignored when `token` is set. Defaults to `KYPO-Client`. Can be set with `KYPO_CLIENT_ID` environmental variable. See [how to get KYPO client_id](https://github.com/vydrazde/terraform-provider-kypo/wiki/How-to-get-KYPO-CRP-client_id).
- `client_id` (String) KYPO local OIDC client ID. Will be ignored when `token` is set. Defaults to `KYPO-Client`. Can be set with `KYPO_CLIENT_ID` environmental variable. See [how to get KYPO client_id](https://registry.terraform.io/vydrazde/kypo/latest/docs/guides/getting_oidc_client_id).
- `endpoint` (String) URI of the homepage of the KYPO instance, like `https://my.kypo.instance.ex`. Can be set with `KYPO_ENDPOINT` environmental variable.
- `password` (String, Sensitive) `password` of the user to login as with `username`. Use either `username` and `password` or just `token`. Can be set with `KYPO_PASSWORD` environmental variable.
- `retry_count` (Number) How many times to retry failed HTTP requests. There is a delay of 100ms before the first retry. For each following retry, the delay is doubled. Defaults to 0. Can be set with `KYPO_RETRY_COUNT` environmental variable.
- `token` (String, Sensitive) Bearer token to be used. Takes precedence before `username` and `password`. Bearer tokens usually have limited lifespan. Can be set with `KYPO_TOKEN` environmental variable.
- `username` (String) `username` of the user to login as with `password`. Use either `username` and `password` or just `token`. Can be set with `KYPO_USERNAME` environmental variable.
56 changes: 39 additions & 17 deletions docs/resources/sandbox_allocation_unit.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,50 +35,72 @@ resource "kypo_sandbox_allocation_unit" "example" {

### Required

- `pool_id` (Number) Id of associated sandbox pool
- `pool_id` (Number) Id of the associated sandbox pool

### Optional

- `warning_on_allocation_failure` (Boolean) If `true`, will emit a warning instead of error when one of the allocation request stages fails.
- `poll_times` (Attributes) Times after which the result of the operation is periodically checked. Times are strings which can be [parsed as a duration](https://pkg.go.dev/time#ParseDuration). (see [below for nested schema](#nestedatt--poll_times))
- `timeouts` (Attributes) (see [below for nested schema](#nestedatt--timeouts))
- `warning_on_allocation_failure` (Boolean) Whether to emit a warning instead of error when one of the allocation request stages fails

### Read-Only

- `allocation_request` (Attributes) Associated allocation request (see [below for nested schema](#nestedatt--allocation_request))
- `cleanup_request` (Attributes) Associated cleanup request (see [below for nested schema](#nestedatt--cleanup_request))
- `created_by` (Attributes) Creator of this sandbox pool (see [below for nested schema](#nestedatt--created_by))
- `id` (Number) Sandbox Allocation Unit Id
- `locked` (Boolean) TODO
- `allocation_request` (Attributes) Allocation request of the allocation unit (see [below for nested schema](#nestedatt--allocation_request))
- `cleanup_request` (Attributes) Cleanup request of the allocation unit (see [below for nested schema](#nestedatt--cleanup_request))
- `created_by` (Attributes) Who created the sandbox allocation unit (see [below for nested schema](#nestedatt--created_by))
- `id` (Number) Id of the sandbox allocation unit
- `locked` (Boolean) Whether the allocation unit is locked. The allocation unit is locked when it is claimed by a Trainee and has an associated training run

<a id="nestedatt--poll_times"></a>
### Nested Schema for `poll_times`

Optional:

- `create` (String) Poll time for awaiting the allocation of the allocation unit, defaults to `10s`. Is used by both `Create` and `Update` operations.
- `delete` (String) Poll time for awaiting the cleanup of the allocation unit, defaults to `5s`.


<a id="nestedatt--timeouts"></a>
### Nested Schema for `timeouts`

Optional:

- `create` (String) A string that can be [parsed as a duration](https://pkg.go.dev/time#ParseDuration) consisting of numbers and unit suffixes, such as "30s" or "2h45m". Valid time units are "s" (seconds), "m" (minutes), "h" (hours).
- `delete` (String) A string that can be [parsed as a duration](https://pkg.go.dev/time#ParseDuration) consisting of numbers and unit suffixes, such as "30s" or "2h45m". Valid time units are "s" (seconds), "m" (minutes), "h" (hours). Setting a timeout for a Delete operation is only applicable if changes are saved into state before the destroy operation occurs.
- `read` (String) A string that can be [parsed as a duration](https://pkg.go.dev/time#ParseDuration) consisting of numbers and unit suffixes, such as "30s" or "2h45m". Valid time units are "s" (seconds), "m" (minutes), "h" (hours). Read operations occur during any refresh or planning operation when refresh is enabled.
- `update` (String) A string that can be [parsed as a duration](https://pkg.go.dev/time#ParseDuration) consisting of numbers and unit suffixes, such as "30s" or "2h45m". Valid time units are "s" (seconds), "m" (minutes), "h" (hours).


<a id="nestedatt--allocation_request"></a>
### Nested Schema for `allocation_request`

Read-Only:

- `allocation_unit_id` (Number) Id of the associated allocation unit
- `created` (String) TODO
- `created` (String) Date and time when the allocation request was created
- `id` (Number) Id of the allocation request
- `stages` (List of String) TODO
- `stages` (List of String) Statuses of the allocation stages. List of three strings, where each is one of `IN_QUEUE`, `FINISHED`, `FAILED` or `RUNNING`


<a id="nestedatt--cleanup_request"></a>
### Nested Schema for `cleanup_request`

Read-Only:

- `allocation_unit_id` (Number) Id of the associated allocation unit
- `created` (String) TODO
- `allocation_unit_id` (Number) Id of the allocation unit
- `created` (String) Date and time when the allocation request was created
- `id` (Number) Id of the cleanup request
- `stages` (List of String) TODO
- `stages` (List of String) Statuses of cleanup stages. List of three strings, where each is one of `IN_QUEUE`, `FINISHED`, `FAILED` or `RUNNING`


<a id="nestedatt--created_by"></a>
### Nested Schema for `created_by`

Read-Only:

- `family_name` (String) TODO
- `full_name` (String) TODO
- `given_name` (String) TODO
- `family_name` (String) Family name of the user
- `full_name` (String) Full name of the user
- `given_name` (String) Given name of the user
- `id` (Number) Id of the user
- `mail` (String) TODO
- `sub` (String) TODO
- `mail` (String) Email of the user
- `sub` (String) Sub of the user as given by an OIDC provider
14 changes: 7 additions & 7 deletions docs/resources/sandbox_definition.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,18 @@ resource "kypo_sandbox_definition" "example" {

### Read-Only

- `created_by` (Attributes) Creator of this sandbox definition (see [below for nested schema](#nestedatt--created_by))
- `id` (Number) Example identifier
- `created_by` (Attributes) Who created the sandbox definition (see [below for nested schema](#nestedatt--created_by))
- `id` (Number) Id of the sandbox definition
- `name` (String) Name of the sandbox definition

<a id="nestedatt--created_by"></a>
### Nested Schema for `created_by`

Read-Only:

- `family_name` (String) TODO
- `full_name` (String) TODO
- `given_name` (String) TODO
- `family_name` (String) Family name of the user
- `full_name` (String) Full name of the user
- `given_name` (String) Given name of the user
- `id` (Number) Id of the user
- `mail` (String) TODO
- `sub` (String) TODO
- `mail` (String) Email of the user
- `sub` (String) Sub of the user as given by an OIDC provider
Loading

0 comments on commit 202c269

Please sign in to comment.