Skip to content

Commit

Permalink
Add rmon_check_group resource for managing check groups
Browse files Browse the repository at this point in the history
This commit introduces a new rmon_check_group resource for managing check groups. Documentation, examples, and the corresponding provider and schema definitions are included to support this new resource.
  • Loading branch information
Aidaho12 committed Nov 17, 2024
1 parent bd0073d commit 63fa524
Show file tree
Hide file tree
Showing 7 changed files with 232 additions and 0 deletions.
38 changes: 38 additions & 0 deletions docs/resources/check_group.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "rmon_check_group Resource - rmon"
subcategory: ""
description: |-
Managing Check groups.
---

# rmon_check_group (Resource)

Managing Check groups.



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

### Required

- `name` (String) The name of the check group.

### Optional

- `group_id` (Number) The user group ID.
- `timeouts` (Block, Optional) (see [below for nested schema](#nestedblock--timeouts))

### Read-Only

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

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

Optional:

- `create` (String)
- `delete` (String)
- `update` (String)
9 changes: 9 additions & 0 deletions examples/resources/check_group/example_1.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
provider "rmon" {
base_url = "https://..."
login = "test"
password = "testpass"
}

resource "rmon_check_group" "example" {
name = "example_group"
}
4 changes: 4 additions & 0 deletions examples/resources/check_group/example_2.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import {
to = rmon_check_group.example
id = "1"
}
1 change: 1 addition & 0 deletions examples/resources/check_group/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
% terraform import rmon_check_group.example 1
1 change: 1 addition & 0 deletions rmon/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func Provider() *schema.Provider {
"rmon_check_http": resourceCheckHttp(),
"rmon_check_smtp": resourceCheckSmtp(),
"rmon_check_rabbitmq": resourceCheckRabbitmq(),
"rmon_check_group": resourceCheckGroup(),
},
DataSourcesMap: map[string]*schema.Resource{
"rmon_group": dataSourceGroup(),
Expand Down
128 changes: 128 additions & 0 deletions rmon/resource_rmon_check_group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package rmon

import (
"context"
"encoding/json"
"fmt"
"log"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func resourceCheckGroup() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourceCheckGroupCreate,
ReadWithoutTimeout: resourceCheckGroupRead,
UpdateWithoutTimeout: resourceCheckGroupUpdate,
DeleteWithoutTimeout: resourceCheckGroupDelete,

Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(10 * time.Minute),
Update: schema.DefaultTimeout(10 * time.Minute),
Delete: schema.DefaultTimeout(30 * time.Minute),
},

Description: "Managing Check groups.",

Schema: map[string]*schema.Schema{
NameField: {
Type: schema.TypeString,
Required: true,
Description: "The name of the check group.",
},
GroupIDField: {
Type: schema.TypeInt,
Optional: true,
Description: "The user group ID.",
},
},
}
}

func resourceCheckGroupCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*Config).Client
name := d.Get(NameField).(string)

requestBody := map[string]interface{}{NameField: name, GroupIDField: d.Get(GroupIDField).(int)}
resp, err := client.doRequest("POST", "/api/v1.0/rmon/check-group", requestBody)
if err != nil {
return diag.FromErr(err)
}

log.Printf("API response: %s", resp)

// Assuming the response contains an ID field with the unique identifier
var result map[string]interface{}
if err := json.Unmarshal(resp, &result); err != nil {
return diag.FromErr(err)
}

id, ok := result[IDField].(float64) // ID возвращается как число
if !ok {
return diag.Errorf("unable to find ID in response: %v", result)
}

d.SetId(fmt.Sprintf("%d", int(id))) // Преобразование ID в строку
return resourceCheckGroupRead(ctx, d, m)
}

func resourceCheckGroupRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*Config).Client
id := d.Id()

// Implement API call to read the resource
resp, err := client.doRequest("GET", fmt.Sprintf("/api/v1.0/rmon/check-group/%s", id), nil)
if err != nil {
return diag.FromErr(err)
}

// Process response and set data
var result map[string]interface{}
if err := json.Unmarshal(resp, &result); err != nil {
return diag.FromErr(err)
}

if name, ok := result[NameField].(string); ok {
d.Set(NameField, name)
}

if groupId, ok := result[GroupIDField].(int); ok {
d.Set(GroupIDField, groupId)
}

return nil
}

func resourceCheckGroupUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*Config).Client
id := d.Id()

requestBody := map[string]interface{}{NameField: d.Get(NameField).(string), GroupIDField: d.Get(GroupIDField).(int)}

_, err := client.doRequest("PUT", fmt.Sprintf("/api/v1.0/rmon/check-group/%s", id), requestBody)
if err != nil {
return diag.FromErr(err)
}

return resourceCheckGroupRead(ctx, d, m)
}

func resourceCheckGroupDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*Config).Client
id := d.Id()

// Implement API call to delete the resource
_, err := client.doRequest("DELETE", fmt.Sprintf("/api/v1.0/rmon/check-group/%s", id), nil)
if err != nil {
return diag.FromErr(err)
}

d.SetId("")
return nil
}
51 changes: 51 additions & 0 deletions templates/resources/check_group.md.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "rmon_check_group Resource - rmon"
subcategory: ""
description: |-
Managing Check groups.
---

# rmon_check_group (Resource)

Managing Check groups.

## Example Usage

{{ tffile "./examples/resources/check_group/example_1.tf" }}


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

### Required

- `name` (String) The name of the check group.

### Optional

- `group_id` (Number) The user group ID.
- `timeouts` (Block, Optional) (see [below for nested schema](#nestedblock--timeouts))

### Read-Only

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

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

Optional:

- `create` (String)
- `delete` (String)
- `update` (String)

## Import

In Terraform v1.7.0 and later, use an import block to import Country. For example:

{{tffile "./examples/resources/check_group/example_2.tf"}}

Using terraform import, import Country can be imported using the `id`, e.g. For example:

{{codefile "shell" "./examples/resources/check_group/import.sh"}}

0 comments on commit 63fa524

Please sign in to comment.