Skip to content

Commit

Permalink
Merge pull request #16 from dheeruk12/main
Browse files Browse the repository at this point in the history
Add support for TeamlevelPermissions ,OutgoingRules
  • Loading branch information
dheeruk12 authored Oct 26, 2023
2 parents 0f4c941 + 74d5898 commit 7f3a866
Show file tree
Hide file tree
Showing 7 changed files with 456 additions and 1 deletion.
81 changes: 81 additions & 0 deletions docs/resources/zenduty_outgoing_rules.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
---
page_title: "Zenduty Outgoing Rules"
subcategory: ""
description: |-
" `zenduty_outgoing_rules` is a resource to manage outgoing rules in a integration"
---
# zenduty_outgoing_rules (Resource)
`zenduty_outgoing_rules` is a resource to manage outgoing rules in a integration


## Example Usage

```hcl
resource "zenduty_teams" "exampleteam" {
name = "exmaple team"
}
resource "zenduty_services" "exampleservice" {
name = "example service"
team_id = zenduty_teams.exampleteam.id
escalation_policy = zenduty_esp.example_esp.id e
}
resource "zenduty_integrations" "exampleintegration" {
team_id = zenduty_teams.exampleteam.id
service_id = zenduty_services.exampleservice.id
application = ""
name = "exampleintegration"
summary = "This is the summary for the example integration"
}
```

```hcl
resource "zenduty_outgoing_rules" "example_outgoingrules" {
team_id = zenduty_teams.exampleteam.id
service_id = zenduty_services.exampleservice.id
integration_id = zenduty_integrations.exampleintegration.id
rule_json = ""
}
```

## Argument Reference

* `team_id` (Required) - The unique_id of the team to create the alert rule in.
* `service_id` (Required) - The unique_id of the service to create the alert rule in.
* `integration_id` (Required) - The unique_id of the integration to create the alert rule in.
* `rule_json` (Required)(string) - The rule json of the alert rule.You cannot construct the rule json in terraform as of now.One can construct the rule json in Zenduty's UI.Create an outgoing rule in Zenduty and copy the rule_json from the UI.
* `enabled` (Optional) - boolean value to enable or disabled


## Attributes Reference

The following attributes are exported:

* `id` - The ID of the Outgoing Rule.

## Import

Integrations can be imported using the `team_id`(ie. unique_id of the team), `service_id`(ie. unique_id of the service),`integration_id`(ie. unique_id of the integration) and `outgoing_rule_id`(ie. unique_id of the alert rule).

```hcl
resource "zenduty_outgoing_rules" "demorule" {
}
```

`$ terraform import zenduty_outgoing_rules.demorule team_id/service_id/integration_id/outgoing_rule_id`

`$ terraform state show zenduty_outgoing_rules.demorule`

`* copy the output data and paste inside zenduty_outgoing_rules.demorule resource block and remove the id attribute`

`$ terraform plan` to verify the import





70 changes: 70 additions & 0 deletions docs/resources/zenduty_team_level_permissions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "Zenduty: TeamLevelPermissions"
subcategory: ""
description: |-
Provides a Zenduty TeamLevelPermissions Resource. This allows TeamLeveLPermissions to be created, updated, deleted.
---

# Resource : zenduty_team_permissions

Provides a Zenduty TeamLeveLPermissions Resource. This allows TeamLeveLPermissions to be created, updated, deleted.


## Example Usage
```hcl
resource "zenduty_teams" "exampleteam" {
name = "exmaple team"
}
```

```hcl
resource "zenduty_team_permissions" "restricted" {
team_id = zenduty_teams.exampleteam.id
permissions = [
"analytics_read",
]
}
```


<!-- schema generated by tfplugindocs -->
## Argument Reference

* `team_id` (Required) - UniqueID of the team
* `permissions` (Required) - List of permissions that your team can give to non-team members within your account

### Permissions

Please refer to the following link for detailed documentation https://docs.zenduty.com/rbac#team-level-permissions

- "analytics_read",
- "escalation_policy_attach",
- "escalation_policy_read",
- "incident_read",
- "incident_role_read",
- "incident_write",
- "integration_read",
- "maintenance_read",
- "member_read",
- "post_incident_task_read",
- "postmortem_read",
- "priority_read",
- "schedule_attach",
- "schedule_read",
- "service_read",
- "sla_read",
- "stakeholder_template_read",
- "tag_read",
- "task_template_read",
- "team_read",


## Attributes Reference

The following attributes are exported:

* `id` - The UniqueID of the Zenduty Team.


2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module terraform-provider-zenduty
go 1.17

require (
github.com/Zenduty/zenduty-go-sdk v0.1.8
github.com/Zenduty/zenduty-go-sdk v0.1.9
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
github.com/hashicorp/terraform-plugin-sdk/v2 v2.10.1
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ github.com/Zenduty/zenduty-go-sdk v0.1.7 h1:IojKvlQAKI6/5GnicYCFLtz7+J6vnG+PsHlU
github.com/Zenduty/zenduty-go-sdk v0.1.7/go.mod h1:XnK1p2uyFpBUwbW+xecKSww0klZOSKKoywobqGDlFE8=
github.com/Zenduty/zenduty-go-sdk v0.1.8 h1:vREmE/XO+nVR7oZ0JLp2W18QTl8r3a6ymsYY7BwDAIg=
github.com/Zenduty/zenduty-go-sdk v0.1.8/go.mod h1:XnK1p2uyFpBUwbW+xecKSww0klZOSKKoywobqGDlFE8=
github.com/Zenduty/zenduty-go-sdk v0.1.9 h1:8sHPvCMyAZL+bRJQvSmweIFCq046OFRE450v5yw/N3s=
github.com/Zenduty/zenduty-go-sdk v0.1.9/go.mod h1:XnK1p2uyFpBUwbW+xecKSww0klZOSKKoywobqGDlFE8=
github.com/acomagu/bufpipe v1.0.3 h1:fxAGrHZTgQ9w5QqVItgzwj235/uYZYgbXitB+dLupOk=
github.com/acomagu/bufpipe v1.0.3/go.mod h1:mxdxdup/WdsKVreO5GpW4+M/1CE2sMG4jeGJ2sYmHc4=
github.com/agext/levenshtein v1.2.1/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558=
Expand Down
2 changes: 2 additions & 0 deletions zenduty/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ func Provider() *schema.Provider {
"zenduty_post_incident_tasks": resourcePostIncidentTasks(),
"zenduty_task_templates": resourceTaskTemplates(),
"zenduty_task_template_tasks": resourceTaskTemplateTaskTasks(),
"zenduty_team_permissions": resourceTeamLevelPermissions(),
"zenduty_outgoing_rules": resourceOutgoingRules(),
},

DataSourcesMap: map[string]*schema.Resource{
Expand Down
167 changes: 167 additions & 0 deletions zenduty/resource_outgoing_rules.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
package zenduty

import (
"context"
"errors"
"fmt"
"strings"

"github.com/Zenduty/zenduty-go-sdk/client"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func resourceOutgoingRules() *schema.Resource {
return &schema.Resource{
CreateContext: resourceCreateOutgoingRules,
UpdateContext: resourceUpdateOutgoingRules,
DeleteContext: resourceDeleteOutgoingRules,
ReadContext: resourceReadOutgoingRules,
Importer: &schema.ResourceImporter{
State: resourceOutgoingRulesImporter,
},
Schema: map[string]*schema.Schema{
"rule_json": {
Type: schema.TypeString,
Required: true,
},
"enabled": {
Type: schema.TypeBool,
Optional: true,
Default: true,
},
"team_id": {
Type: schema.TypeString,
Required: true,
ValidateDiagFunc: ValidateUUID(),
},
"service_id": {
Type: schema.TypeString,
Required: true,
ValidateDiagFunc: ValidateUUID(),
},
"integration_id": {
Type: schema.TypeString,
Required: true,
ValidateDiagFunc: ValidateUUID(),
},
},
}
}

func ValidateAncCreateOutgoingRules(Ctx context.Context, d *schema.ResourceData, m interface{}) (*client.OutgoingRule, diag.Diagnostics) {
newAlertRule := &client.OutgoingRule{}

if v, ok := d.GetOk("rule_json"); ok {
newAlertRule.RuleJSON = v.(string)
}
enabled := d.Get("enabled")
newAlertRule.Enabled = enabled.(bool)

if newAlertRule.RuleJSON == "" {
return nil, diag.FromErr(errors.New("rule_json is required"))
}
if !isJSONString(newAlertRule.RuleJSON) {
return nil, diag.FromErr(errors.New("rule_json is not valid JSON"))
}

return newAlertRule, nil
}

func resourceCreateOutgoingRules(Ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
apiclient, _ := m.(*Config).Client()
var diags diag.Diagnostics
var teamID, serviceID, integrationID string

teamID = d.Get("team_id").(string)
serviceID = d.Get("service_id").(string)
integrationID = d.Get("integration_id").(string)

newRule, ruleErr := ValidateAncCreateOutgoingRules(Ctx, d, m)
if ruleErr != nil {
return ruleErr
}

alertRule, err := apiclient.OutgoingRules.CreateOutgoingRule(teamID, serviceID, integrationID, newRule)

if err != nil {
return diag.FromErr(err)
}
d.SetId(alertRule.UniqueID)
return diags
}

func resourceUpdateOutgoingRules(Ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
apiclient, _ := m.(*Config).Client()
var diags diag.Diagnostics
var teamID, serviceID, integrationID string

teamID = d.Get("team_id").(string)
serviceID = d.Get("service_id").(string)
integrationID = d.Get("integration_id").(string)

newRule, ruleErr := ValidateAncCreateOutgoingRules(Ctx, d, m)
if ruleErr != nil {
return ruleErr
}

_, err := apiclient.OutgoingRules.UpdateOutgoingRule(teamID, serviceID, integrationID, d.Id(), newRule)
if err != nil {
return diag.FromErr(err)
}

return diags
}

func resourceReadOutgoingRules(Ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
apiclient, _ := m.(*Config).Client()
var diags diag.Diagnostics
var teamID, serviceID, integrationID string

teamID = d.Get("team_id").(string)
serviceID = d.Get("service_id").(string)
integrationID = d.Get("integration_id").(string)

rule, err := apiclient.OutgoingRules.GetOutgoingRule(teamID, serviceID, integrationID, d.Id())
if err != nil {
return diag.FromErr(err)
}
d.SetId(rule.UniqueID)
d.Set("rule_json", rule.RuleJSON)
d.Set("enabled", rule.Enabled)

return diags
}

func resourceDeleteOutgoingRules(Ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics
apiclient, _ := m.(*Config).Client()
teamID, serviceID, integrationID, uniqueID := d.Get("team_id").(string), d.Get("service_id").(string), d.Get("integration_id").(string), d.Id()

err := apiclient.OutgoingRules.DeleteOutgoingRule(teamID, serviceID, integrationID, uniqueID)
if err != nil {
return diag.FromErr(err)
}
return diags
}

func resourceOutgoingRulesImporter(d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) {
parts := strings.Split(d.Id(), "/")
if len(parts) != 4 {
return nil, fmt.Errorf("unexpected format of id (%q), expected <team_id>/<service_id>/<integration_id>/<alert_rule_id>", d.Id())
} else if !IsValidUUID(parts[0]) {
return nil, fmt.Errorf("invalid team_id (%q)", parts[0])
} else if !IsValidUUID(parts[1]) {
return nil, fmt.Errorf("invalid serviceid (%q)", parts[1])
} else if !IsValidUUID(parts[2]) {
return nil, fmt.Errorf("invalid integration_id (%q)", parts[2])
} else if !IsValidUUID(parts[3]) {
return nil, fmt.Errorf("invalid alert_rule_id (%q)", parts[3])
}
d.SetId(parts[3])
d.Set("integration_id", parts[2])
d.Set("team_id", parts[0])
d.Set("service_id", parts[1])

return []*schema.ResourceData{d}, nil
}
Loading

0 comments on commit 7f3a866

Please sign in to comment.