-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add rmon_check_group resource for managing check groups
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
Showing
7 changed files
with
232 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { | ||
to = rmon_check_group.example | ||
id = "1" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
% terraform import rmon_check_group.example 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"}} |