Skip to content

Commit

Permalink
Initial implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy Royle committed Jan 14, 2022
1 parent 884027b commit 6534f83
Show file tree
Hide file tree
Showing 24 changed files with 463 additions and 263 deletions.
17 changes: 1 addition & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,4 @@
# Terraform Provider Scaffolding (Terraform Plugin SDK)

_This template repository is built on the [Terraform Plugin SDK](https://github.com/hashicorp/terraform-plugin-sdk). The template repository built on the [Terraform Plugin Framework](https://github.com/hashicorp/terraform-plugin-framework) can be found at [terraform-provider-scaffolding-framework](https://github.com/hashicorp/terraform-provider-scaffolding-framework). See [Which SDK Should I Use?](https://www.terraform.io/docs/plugin/which-sdk.html) in the Terraform documentation for additional information._

This repository is a *template* for a [Terraform](https://www.terraform.io) provider. It is intended as a starting point for creating Terraform providers, containing:

- A resource, and a data source (`internal/provider/`),
- Examples (`examples/`) and generated documentation (`docs/`),
- Miscellaneous meta files.

These files contain boilerplate code that you will need to edit to create your own Terraform provider. Tutorials for creating Terraform providers can be found on the [HashiCorp Learn](https://learn.hashicorp.com/collections/terraform/providers) platform.

Please see the [GitHub template repository documentation](https://help.github.com/en/github/creating-cloning-and-archiving-repositories/creating-a-repository-from-a-template) for how to create a new repository from this template on GitHub.

Once you've written your provider, you'll want to [publish it on the Terraform Registry](https://www.terraform.io/docs/registry/providers/publishing.html) so that others can use it.

# Terraform Provider MarkLogic

## Requirements

Expand Down
30 changes: 0 additions & 30 deletions docs/data-sources/data_source.md

This file was deleted.

37 changes: 37 additions & 0 deletions docs/data-sources/user.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "marklogic_user Data Source - terraform-provider-marklogic"
subcategory: ""
description: |-
Sample data source in the Terraform provider scaffolding.
---

# marklogic_user (Data Source)

Sample data source in the Terraform provider scaffolding.

## Example Usage

```terraform
data "marklogic_user" "test-user" {
name = "test-user"
}
```

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

### Required

- **name** (String) The user's name.

### Optional

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

### Read-Only

- **description** (String) A description for the user.
- **roles** (Set of String) The roles assigned to the user.


18 changes: 14 additions & 4 deletions docs/index.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
---
page_title: "scaffolding Provider"
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "marklogic Provider"
subcategory: ""
description: |-
---

# scaffolding Provider
# marklogic Provider



## Example Usage

```terraform
provider "scaffolding" {
# example configuration here
provider "marklogic" {
username = "admin" // or MARKLOGIC_USERNAME
password = "foobar1234" // or MARKLOGIC_PASSWORD
base_url = "https://my.marklogic.host:8002" // or MARKLOGIC_BASE_URL
}
```

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

### Optional

- **base_url** (String)
- **password** (String)
- **username** (String)
27 changes: 0 additions & 27 deletions docs/resources/resource.md

This file was deleted.

37 changes: 37 additions & 0 deletions docs/resources/user.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "marklogic_user Resource - terraform-provider-marklogic"
subcategory: ""
description: |-
A user in MarkLogic.
---

# marklogic_user (Resource)

A user in MarkLogic.

## Example Usage

```terraform
resource "marklogic_user" "test-user" {
name = "test-user"
roles = ["manage-user"]
password = "foobar1234"
}
```

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

### Required

- **name** (String) The user's name.
- **password** (String, Sensitive) The user's password.
- **roles** (List of String) The roles to assign to the user.

### Optional

- **description** (String) A description for the user.
- **id** (String) The ID of this resource.


3 changes: 3 additions & 0 deletions examples/data-sources/marklogic_user/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
data "marklogic_user" "test-user" {
name = "test-user"
}
3 changes: 0 additions & 3 deletions examples/data-sources/scaffolding_data_source/data-source.tf

This file was deleted.

8 changes: 5 additions & 3 deletions examples/provider/provider.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
provider "scaffolding" {
# example configuration here
}
provider "marklogic" {
username = "admin" // or MARKLOGIC_USERNAME
password = "foobar1234" // or MARKLOGIC_PASSWORD
base_url = "https://my.marklogic.host:8002" // or MARKLOGIC_BASE_URL
}
5 changes: 5 additions & 0 deletions examples/resources/marklogic_user/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
resource "marklogic_user" "test-user" {
name = "test-user"
roles = ["manage-user"]
password = "foobar1234"
}
3 changes: 0 additions & 3 deletions examples/resources/scaffolding_resource/resource.tf

This file was deleted.

3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
module github.com/hashicorp/terraform-provider-scaffolding
module github.com/healx/terraform-provider-marklogic

go 1.15

require (
github.com/hashicorp/terraform-plugin-docs v0.5.1
github.com/hashicorp/terraform-plugin-log v0.2.1
github.com/hashicorp/terraform-plugin-sdk/v2 v2.10.1
github.com/judgegregg/go-http-digest-auth-client v0.6.1
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,8 @@ github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/jstemmer/go-junit-report v0.9.1 h1:6QPYqodiu3GuPL+7mfx+NwDdp2eTkp9IfEUpgAwUN0o=
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
github.com/judgegregg/go-http-digest-auth-client v0.6.1 h1:r2N/KgSd0uS18kyoKSVSI3RQ+Rr6Wynk4iaaZUnjqq4=
github.com/judgegregg/go-http-digest-auth-client v0.6.1/go.mod h1:Eq3vslaiSqDvLLWgenX2mpHiZHG0ZQSKT1Li5Vo5iaQ=
github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351 h1:DowS9hvgyYSX4TO5NpyC606/Z4SxnNYbT+WX27or6Ck=
github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
Expand Down
101 changes: 101 additions & 0 deletions internal/provider/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package provider

import (
"context"
"encoding/json"
"fmt"
"io"

"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
dac "github.com/judgegregg/go-http-digest-auth-client"
)

func (a *apiClient) GetUser(ctx context.Context, username string) (map[string]interface{}, diag.Diagnostics) {

req := dac.NewRequest(a.username, a.password, "GET", fmt.Sprintf("%s/manage/v2/users/%s/properties", a.baseUrl, username), "")
req.Header.Set("Accept", "application/json")
req.Header.Set("User-Agent", a.userAgent)

tflog.Trace(ctx, fmt.Sprintf("Calling api %s", fmt.Sprintf("%s/manage/v2/users/%s/properties", a.baseUrl, username)))
tflog.Trace(ctx, fmt.Sprintf("HTTP Headers %s", req.Header))

resp, err := req.Execute()
if err != nil {
return nil, diag.FromErr(err)
}
defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)

var result map[string]interface{}

tflog.Trace(ctx, string(body))

err = json.Unmarshal([]byte(body), &result)

if err != nil {
return nil, diag.FromErr(err)
}

return result, nil
}

func (a *apiClient) CreateUser(ctx context.Context, user map[string]interface{}) diag.Diagnostics {
payload, err := json.Marshal(user)

if err != nil {
return diag.FromErr(err)
}

req := dac.NewRequest(a.username, a.password, "POST", fmt.Sprintf("%s/manage/v2/users", a.baseUrl), string(payload))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("User-Agent", a.userAgent)

tflog.Trace(ctx, fmt.Sprintf("Calling api %s", fmt.Sprintf("%s/manage/v2/users", a.baseUrl)))
tflog.Trace(ctx, fmt.Sprintf("HTTP Headers %s", req.Header))

_, err = req.Execute()
if err != nil {
return diag.FromErr(err)
}

return nil
}

func (a *apiClient) DeleteUser(ctx context.Context, username string) diag.Diagnostics {
req := dac.NewRequest(a.username, a.password, "DELETE", fmt.Sprintf("%s/manage/v2/users/%s", a.baseUrl, username), "")
req.Header.Set("User-Agent", a.userAgent)

tflog.Trace(ctx, fmt.Sprintf("Calling api %s", fmt.Sprintf("%s/manage/v2/users/%s", a.baseUrl, username)))
tflog.Trace(ctx, fmt.Sprintf("HTTP Headers %s", req.Header))

_, err := req.Execute()
if err != nil {
return diag.FromErr(err)
}

return nil
}

func (a *apiClient) UpdateUser(ctx context.Context, user map[string]interface{}) diag.Diagnostics {
payload, err := json.Marshal(user)

if err != nil {
return diag.FromErr(err)
}

req := dac.NewRequest(a.username, a.password, "PUT", fmt.Sprintf("%s/manage/v2/users/%s/properties", a.baseUrl, user["user-name"]), string(payload))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("User-Agent", a.userAgent)

tflog.Trace(ctx, fmt.Sprintf("Calling api %s", fmt.Sprintf("%s/manage/v2/users/%s/properties", a.baseUrl, user["user-name"])))
tflog.Trace(ctx, fmt.Sprintf("HTTP Headers %s", req.Header))

_, err = req.Execute()
if err != nil {
return diag.FromErr(err)
}

return nil
}
36 changes: 0 additions & 36 deletions internal/provider/data_source_scaffolding.go

This file was deleted.

Loading

0 comments on commit 6534f83

Please sign in to comment.