Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions docs/resources/capability_webhook_global.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "sonatyperepo_capability_webhook_global Resource - sonatyperepo"
subcategory: ""
description: |-
Manage Capability: Webhook Global
NOTE: Requires Sonatype Nexus Repostiory 3.84.0 or later.
---

# sonatyperepo_capability_webhook_global (Resource)

Manage Capability: Webhook Global

**NOTE:** Requires Sonatype Nexus Repostiory 3.84.0 or later.

## Example Usage

```terraform
resource "sonatyperepo_capability_webhook_global" "webhook" {
enabled = true
notes = "These are notes from Terraform"
properties = {
names = [
"repository"
]
url = "https://test.tld"
secret = "super-secret-key"
}
}
```

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

### Required

- `enabled` (Boolean) Whether the Capability is enabled.
- `properties` (Attributes) Properties specific to this Capability type (see [below for nested schema](#nestedatt--properties))

### Optional

- `notes` (String) Optional notes about configured capability.

### Read-Only

- `id` (String) The internal ID of the Capability.
- `last_updated` (String)

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

Required:

- `names` (Set of String) Event types which trigger this Webhook.
- `url` (String) Send a HTTP POST request to this URL.

Optional:

- `secret` (String, Sensitive) Key to use for HMAC payload digest.

## Import

Import is supported using the following syntax:

```shell
# Import an existing Webhook (Global) Capability into Terraform State.
#
# NOTE: The CAPABILITY_ID can be obtained from the datasource.

# Example
terraform import sonatyperepo_capability_webhook_global.webhook CAPABILITY_ID
```
72 changes: 72 additions & 0 deletions docs/resources/capability_webhook_repository.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "sonatyperepo_capability_webhook_repository Resource - sonatyperepo"
subcategory: ""
description: |-
Manage Capability: Webhook Repository
NOTE: Requires Sonatype Nexus Repostiory 3.84.0 or later.
---

# sonatyperepo_capability_webhook_repository (Resource)

Manage Capability: Webhook Repository

**NOTE:** Requires Sonatype Nexus Repostiory 3.84.0 or later.

## Example Usage

```terraform
resource "sonatyperepo_capability_webhook_repository" "webhook" {
enabled = true
notes = "These are notes from Terraform"
properties = {
names = ["component"]
url = "https://test.tld"
secret = "testing"
repository = "maven-central"
}
}
```

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

### Required

- `enabled` (Boolean) Whether the Capability is enabled.
- `properties` (Attributes) Properties specific to this Capability type (see [below for nested schema](#nestedatt--properties))

### Optional

- `notes` (String) Optional notes about configured capability.

### Read-Only

- `id` (String) The internal ID of the Capability.
- `last_updated` (String)

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

Required:

- `names` (Set of String) Event types which trigger this Webhook.
- `repository` (String) Repository to discriminate events from.
- `url` (String) Send a HTTP POST request to this URL.

Optional:

- `secret` (String, Sensitive) Key to use for HMAC payload digest.

## Import

Import is supported using the following syntax:

```shell
# Import an existing Webhook (Repository) Capability into Terraform State.
#
# NOTE: The CAPABILITY_ID can be obtained from the datasource.

# Example
terraform import sonatyperepo_capability_webhook_repository.webhook CAPABILITY_ID
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Import an existing Webhook (Global) Capability into Terraform State.
#
# NOTE: The CAPABILITY_ID can be obtained from the datasource.

# Example
terraform import sonatyperepo_capability_webhook_global.webhook CAPABILITY_ID
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
resource "sonatyperepo_capability_webhook_global" "webhook" {
enabled = true
notes = "These are notes from Terraform"
properties = {
names = [
"repository"
]
url = "https://test.tld"
secret = "super-secret-key"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Import an existing Webhook (Repository) Capability into Terraform State.
#
# NOTE: The CAPABILITY_ID can be obtained from the datasource.

# Example
terraform import sonatyperepo_capability_webhook_repository.webhook CAPABILITY_ID
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
resource "sonatyperepo_capability_webhook_repository" "webhook" {
enabled = true
notes = "These are notes from Terraform"
properties = {
names = ["component"]
url = "https://test.tld"
secret = "testing"
repository = "maven-central"
}
}
2 changes: 2 additions & 0 deletions internal/provider/capability/capability_common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ const (
resourceFirewallAuditQuarantine string = "sonatyperepo_capability_firewall_audit_and_quarantine"
resourceUiBranding string = "sonatyperepo_capability_ui_branding"
resourceUiSettings string = "sonatyperepo_capability_ui_settings"
resourceWebhookGlobal string = "sonatyperepo_capability_webhook_global"
resourceWebhookRepository string = "sonatyperepo_capability_webhook_repository"
)
58 changes: 57 additions & 1 deletion internal/provider/capability/capability_type/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,22 @@ import (
"context"
"fmt"
"net/http"
"regexp"
"terraform-provider-sonatyperepo/internal/provider/common"

"github.com/hashicorp/terraform-plugin-framework-validators/setvalidator"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"

v3 "github.com/sonatype-nexus-community/nexus-repo-api-client-go/v3"
)

// BaseCapabilityType that all task types build from
// --------------------------------------------
// BaseCapabilityType that all capability types build from
// --------------------------------------------
type BaseCapabilityType struct {
capabilityType common.CapabilityType
Expand Down Expand Up @@ -60,6 +66,7 @@ func (ct *BaseCapabilityType) GetType() common.CapabilityType {
return ct.capabilityType
}

// --------------------------------------------
// CapabilityTypeI that all Capability Types must implement
// --------------------------------------------
type CapabilityTypeI interface {
Expand All @@ -78,3 +85,52 @@ type CapabilityTypeI interface {
UpdateStateFromApi(state any, api any) any
UpdateStateFromPlanForUpdate(plan any, state any) any
}

// --------------------------------------------
// Helper method to generate schema for Webhook Capabilities
// --------------------------------------------
func getPropertiesSchemaForWebhookCapability(permissibleEventTypes []string, includeRepository bool) map[string]schema.Attribute {
defaultProps := map[string]schema.Attribute{
"names": schema.SetAttribute{
Description: "Event types which trigger this Webhook.",
Required: true,
ElementType: types.StringType,
Validators: []validator.Set{
setvalidator.SizeBetween(1, 2),
setvalidator.ValueStringsAre(
stringvalidator.OneOf(permissibleEventTypes...),
),
},
},
"secret": schema.StringAttribute{
Description: "Key to use for HMAC payload digest.",
Optional: true,
Sensitive: true,
},
"url": schema.StringAttribute{
Description: "Send a HTTP POST request to this URL.",
Required: true,
Validators: []validator.String{
stringvalidator.RegexMatches(
regexp.MustCompile(`^https?://[^\s]+$`),
"Must be a valid http:// or https:// URL",
),
},
},
}

if includeRepository {
defaultProps["repository"] = schema.StringAttribute{
Description: "Repository to discriminate events from.",
Required: true,
Validators: []validator.String{
stringvalidator.RegexMatches(
regexp.MustCompile(`^[a-zA-Z0-9\-]{1}[a-zA-Z0-9_\-\.]*$`),
"Must be a valid repository name",
),
},
}
}

return defaultProps
}
104 changes: 104 additions & 0 deletions internal/provider/capability/capability_type/webhook_global.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* Copyright (c) 2019-present Sonatype, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package capabilitytype

import (
"context"
"net/http"
"terraform-provider-sonatyperepo/internal/provider/common"
"terraform-provider-sonatyperepo/internal/provider/model"
"time"

"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
v3 "github.com/sonatype-nexus-community/nexus-repo-api-client-go/v3"
)

// --------------------------------------------
// Capabiltiy Type: Webhook: Global
// --------------------------------------------
type WebhookGlobalCapability struct {
BaseCapabilityType
}

func NewWebhookGlobalCapability() *WebhookGlobalCapability {
return &WebhookGlobalCapability{
BaseCapabilityType: BaseCapabilityType{
capabilityType: common.CAPABILITY_TYPE_WEBHOOK_GLOBAL,
publicName: "Webhook Global",
},
}
}

// --------------------------------------------
// Capabiltiy Type: Webhook: Repository
// --------------------------------------------
func (f *WebhookGlobalCapability) DoCreateRequest(plan any, apiClient *v3.APIClient, ctx context.Context, version common.SystemVersion) (*v3.CapabilityDTO, *http.Response, error) {
// Cast to correct Plan Model Type
planModel := (plan).(model.WebhookGlobalCapabilityModel)

// Call API to Create
return apiClient.CapabilitiesAPI.Create3(ctx).Body(*planModel.ToApiCreateModel(version)).Execute()
}

func (f *WebhookGlobalCapability) DoUpdateRequest(plan any, capabilityId string, apiClient *v3.APIClient, ctx context.Context, version common.SystemVersion) (*http.Response, error) {
// Cast to correct Plan Model Type
planModel := (plan).(model.WebhookGlobalCapabilityModel)

// Call API to Update
return apiClient.CapabilitiesAPI.Update3(ctx, capabilityId).Body(*planModel.ToApiUpdateModel(version)).Execute()
}

func (f *WebhookGlobalCapability) GetPlanAsModel(ctx context.Context, plan tfsdk.Plan) (any, diag.Diagnostics) {
var planModel model.WebhookGlobalCapabilityModel
return planModel, plan.Get(ctx, &planModel)
}

func (f *WebhookGlobalCapability) GetPropertiesSchema() map[string]schema.Attribute {
return getPropertiesSchemaForWebhookCapability(common.AllGlobalWebHookEventTypes(), false)
}

func (f *WebhookGlobalCapability) GetStateAsModel(ctx context.Context, state tfsdk.State) (any, diag.Diagnostics) {
var stateModel model.WebhookGlobalCapabilityModel
return stateModel, state.Get(ctx, &stateModel)
}

func (f *WebhookGlobalCapability) UpdatePlanForState(plan any) any {
var planModel = (plan).(model.WebhookGlobalCapabilityModel)
planModel.LastUpdated = types.StringValue(time.Now().Format(time.RFC850))
return planModel
}

func (f *WebhookGlobalCapability) UpdateStateFromApi(state any, api any) any {
stateModel := (state).(model.WebhookGlobalCapabilityModel)
apiModel := (api).(*v3.CapabilityDTO)
stateModel.FromApiModel(apiModel)
stateModel.LastUpdated = types.StringValue(time.Now().Format(time.RFC850))
return stateModel
}

func (f *WebhookGlobalCapability) UpdateStateFromPlanForUpdate(plan any, state any) any {
planModel := (plan).(model.WebhookGlobalCapabilityModel)
stateModel := (state).(model.WebhookGlobalCapabilityModel)

planModel.Id = stateModel.Id
planModel.LastUpdated = types.StringValue(time.Now().Format(time.RFC850))

return planModel
}
Loading