Skip to content

Commit

Permalink
Merge pull request #207 from ponkuma123/EUM_TERRAFORM_PROVIDER_BRANCH
Browse files Browse the repository at this point in the history
Credential Profile Resource and Data Source.
  • Loading branch information
jim-billy-zoho authored Oct 31, 2023
2 parents 6e027e1 + 8d9f07e commit b9723db
Show file tree
Hide file tree
Showing 20 changed files with 907 additions and 66 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ A terraform provider for managing the following resources in Site24x7:
- User - [site24x7_user](examples/user_us.tf) ([Site24x7 User API doc](https://www.site24x7.com/help/api/#users))
- Tag - [site24x7_tag](examples/tag_us.tf) ([Site24x7 Tag API doc](https://www.site24x7.com/help/api/#tags))
- Schedule Maintenance - [site24x7_schedule_maintenance](examples/schedule_maintenance_us.tf) ([Site24x7 Schedule Maintenance API doc](https://www.site24x7.com/help/api/#schedule-maintenances))
- Credential Profile - [site24x7_credential_profile](examples/credential_profiles_us.tf) ([Credential Profile Terraform doc](https://www.site24x7.com/help/api/#credential-profiles))

#### Integrations

Expand All @@ -84,6 +85,7 @@ A terraform provider for managing the following resources in Site24x7:
- MSP - [site24x7_msp](examples/data-sources/msp_data_source_us.tf) ([MSP API doc](https://registry.terraform.io/providers/site24x7/site24x7/latest/docs/data-sources/msp))
- AWS External ID - [site24x7_aws_external_id](examples/data-sources/aws_external_id_data_source_us.tf) ([AWS External ID Terraform doc](https://registry.terraform.io/providers/site24x7/site24x7/latest/docs/data-sources/aws_external_id))
- Device Key - [site24x7_device_key](examples/data-sources/device_key_data_source_us.tf) ([Device Key Terraform doc](https://registry.terraform.io/providers/site24x7/site24x7/latest/docs/data-sources/device_key))
- Credential Profile - [site24x7_credential_profile](examples/data-sources/credential_profile_data_source_us.tf) ([Credential Profile Terraform doc](https://registry.terraform.io/providers/site24x7/site24x7/latest/docs/data-sources/credential_profile))

Usage example
-------------
Expand Down
83 changes: 83 additions & 0 deletions api/endpoints/common/credential_profiles_impl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package common

import (
"github.com/site24x7/terraform-provider-site24x7/api"
"github.com/site24x7/terraform-provider-site24x7/rest"
)

type CredentialProfile interface {
Get(credentialProfileID string) (*api.CredentialProfile, error)
Create(credentialProfile *api.CredentialProfile) (*api.CredentialProfile, error)
Update(credentialProfile *api.CredentialProfile) (*api.CredentialProfile, error)
Delete(credentialProfileID string) error
ListWebCredentials() ([]*api.CredentialProfile, error)
}

type credentialprofile struct {
client rest.Client
}

func NewCredentialProfile(client rest.Client) CredentialProfile {
return &credentialprofile{
client: client,
}
}

func (c *credentialprofile) Get(credentialProfileID string) (*api.CredentialProfile, error) {
credentialProfile := &api.CredentialProfile{}
err := c.client.
Get().
Resource("credential_profile").
ResourceID(credentialProfileID).
Do().
Parse(credentialProfile)

return credentialProfile, err
}

func (c *credentialprofile) Create(credentialProfile *api.CredentialProfile) (*api.CredentialProfile, error) {
newCredentialProfile := &api.CredentialProfile{}
err := c.client.
Post().
Resource("credential_profile").
AddHeader("Content-Type", "application/json;charset=UTF-8").
Body(credentialProfile).
Do().
Parse(newCredentialProfile)

return newCredentialProfile, err
}

func (c *credentialprofile) Update(credentialProfile *api.CredentialProfile) (*api.CredentialProfile, error) {
updatedCredentialProfile := &api.CredentialProfile{}
err := c.client.
Put().
Resource("credential_profile").
ResourceID(credentialProfile.ID).
AddHeader("Content-Type", "application/json;charset=UTF-8").
Body(credentialProfile).
Do().
Parse(updatedCredentialProfile)

return updatedCredentialProfile, err
}

func (c *credentialprofile) Delete(credentialProfileID string) error {
return c.client.
Delete().
Resource("credential_profile").
ResourceID(credentialProfileID).
Do().
Err()
}

func (c *credentialprofile) ListWebCredentials() ([]*api.CredentialProfile, error) {
credentialProfiles := []*api.CredentialProfile{}
err := c.client.
Get().
Resource("credential_profiles").
Do().
Parse(&credentialProfiles)

return credentialProfiles, err
}
84 changes: 84 additions & 0 deletions api/endpoints/common/credential_profiles_impl_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package common

import (
"testing"

"github.com/site24x7/terraform-provider-site24x7/api"
"github.com/site24x7/terraform-provider-site24x7/rest"
"github.com/site24x7/terraform-provider-site24x7/validation"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestRestApiMonitors(t *testing.T) {
validation.RunTests(t, []*validation.EndpointTest{
{
Name: "Create credential profile",
ExpectedVerb: "POST",
ExpectedPath: "/credential_profiles",
ExpectedBody: validation.Fixture(t, "requests/create_rest_api_monitor.json"),
StatusCode: 200,
ResponseBody: validation.JsonAPIResponseBody(t, nil),
Fn: func(t *testing.T, c rest.Client) {
credentialProfile := &api.CredentialProfile{
CredentialType: 3,
CredentialName: "Creditial profile",
UserName: "postman",
Password: "test",
}

_, err := NewCredentialProfile(c).Create(credentialProfile)
require.NoError(t, err)
},
},
{
Name: "Get Credential profile",
ExpectedVerb: "GET",
ExpectedPath: "/credential_profiles/123",
StatusCode: 200,
ResponseBody: validation.Fixture(t, "responses/get_rest_api_monitor.json"),
Fn: func(t *testing.T, c rest.Client) {
credentialProfile, err := NewCredentialProfile(c).Get("123")
require.NoError(t, err)

expected := &api.CredentialProfile{
CredentialType: 3,
CredentialName: "Creditial profile",
UserName: "postman",
Password: "test",
}

assert.Equal(t, expected, credentialProfile)
},
},
{
Name: "Update Credential profile",
ExpectedVerb: "PUT",
ExpectedPath: "/credential_profiles/123",
ExpectedBody: validation.Fixture(t, "requests/update_rest_api_monitor.json"),
StatusCode: 200,
ResponseBody: validation.JsonAPIResponseBody(t, nil),
Fn: func(t *testing.T, c rest.Client) {
credentialProfile := &api.CredentialProfile{
ID: "123",
CredentialType: 3,
CredentialName: "Creditial profile",
UserName: "postman",
Password: "test",
}

_, err := NewCredentialProfile(c).Update(credentialProfile)
require.NoError(t, err)
},
},
{
Name: "Delete Credential profile",
ExpectedVerb: "DELETE",
ExpectedPath: "/credential_profiles/123",
StatusCode: 200,
Fn: func(t *testing.T, c rest.Client) {
require.NoError(t, NewCredentialProfile(c).Delete("123"))
},
},
})
}
42 changes: 42 additions & 0 deletions api/endpoints/fake/credential_profiles.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package fake

import (
"github.com/site24x7/terraform-provider-site24x7/api"
"github.com/site24x7/terraform-provider-site24x7/api/endpoints/common"
"github.com/stretchr/testify/mock"
)

var _ common.CredentialProfile = &CredentialProfile{}

type CredentialProfile struct {
mock.Mock
}

func (e *CredentialProfile) Get(credentialProfileID string) (*api.CredentialProfile, error) {
args := e.Called(credentialProfileID)
if obj, ok := args.Get(0).(*api.CredentialProfile); ok {
return obj, args.Error(1)
}
return nil, args.Error(1)
}

func (e *CredentialProfile) Create(credentialProfile *api.CredentialProfile) (*api.CredentialProfile, error) {
args := e.Called(credentialProfile)
if obj, ok := args.Get(0).(*api.CredentialProfile); ok {
return obj, args.Error(1)
}
return nil, args.Error(1)
}

func (e *CredentialProfile) Update(credentialProfile *api.CredentialProfile) (*api.CredentialProfile, error) {
args := e.Called(credentialProfile)
if obj, ok := args.Get(0).(*api.CredentialProfile); ok {
return obj, args.Error(1)
}
return nil, args.Error(1)
}

func (e *CredentialProfile) Delete(credentialProfileID string) error {
args := e.Called(credentialProfileID)
return args.Error(0)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"credential_type": 3,
"credential_name": "Credential profile",
"username": "UserName",
"password": "password"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"credential_profile_id":"123",
"credential_type": 3,
"credential_name": "Credential profile",
"username": "UserName",
"password": "password"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"code": 0,
"message": "success",
"data": {
"credential_type": 3,
"credential_name": "Credential profile",
"credential_profile_id": "123",
"username": "UserName",
"password": "password",
"is_credential_associated": false
}
}
2 changes: 1 addition & 1 deletion api/endpoints/threshold_profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (c *thresholdProfiles) Get(profileID string) (*api.ThresholdProfile, error)
err := c.client.
Get().
Resource("threshold_profiles").
SetHeader("Accept", "application/json;").
SetHeader("Accept", "application/json; version=2.1").
ResourceID(profileID).
Do().
Parse(profile)
Expand Down
13 changes: 13 additions & 0 deletions api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,3 +462,16 @@ type DeviceKey struct {
func (deviceKey *DeviceKey) String() string {
return ToString(deviceKey)
}

type CredentialProfile struct {
_ struct{} `type:"structure"` // Enforces key based initialization.
ID string `json:"credential_profile_id"`
CredentialType int `json:"credential_type"`
CredentialName string `json:"credential_name"`
UserName string `json:"username"`
Password string `json:"password"`
}

func (credentialProfile *CredentialProfile) String() string {
return ToString(credentialProfile)
}
61 changes: 61 additions & 0 deletions docs/data-sources/credential_profile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
---
layout: "site24x7"
page_title: "Site24x7: site24x7_credential_profile"
sidebar_current: "docs-site24x7-data-source-credential-profile"
description: |-
Get information about a Credential Profile in Site24x7.
---

# Data Source: site24x7\_credential\_profile

Use this data source to retrieve information about an existing Credential Profiles in Site24x7.

## Example Usage

```hcl
// Data source to fetch an Credential Profile
data "site24x7_credential_profile" "s247credentialprofile" {
// (Required) Regular expression denoting the name of the Credential Profile.
name_regex = "url"
}
// Displays the Credential Profile ID
output "s247_credential_profile_id" {
description = "Credential Profile ID : "
value = data.site24x7_credential_profile.s247credentialprofile.id
}
// Displays the Credential Profile Name
output "s247_credential_profile_name" {
description = "Credential Profile name : "
value = data.site24x7_credential_profile.s247credentialprofile.credential_name
}
// Displays the Credential Profile Type
output "s247_credential_profile_type" {
description = "Credential Profile type : "
value = data.site24x7_credential_profile.s247credentialprofile.credential_type
}
// Displays the Credential Profile username
output "s247_credential_profile_username" {
description = "Credential Profile username: "
value = data.site24x7_credential_profile.s247credentialprofile.username
}
```

## Attributes Reference

### Required

* `name_regex` (String) Regular expression denoting the name of the Credential Profile.

### Read-Only

* `id` (String) The ID of the matching Credential Profile.
* `credential_type` (Integer) Type for the Credential Profile.
* `credential_name` (String) Name for the Credential Profile.
* `username` (String) Username for the Credential Profile.

41 changes: 41 additions & 0 deletions docs/resources/credential_profile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
layout: "site24x7"
page_title: "Site24x7: site24x7_credential_profile"
sidebar_current: "docs-site24x7-resource-credential-profile"
description: |-
Create and manage a Credential Profile monitor in Site24x7.
---

# Resource: site24x7\_Credential\_Profile

Use this resource to create, update and delete a Credential Profile in Site24x7.

## Example Usage

```hcl
// Site24x7 Credential Profile API doc - https://www.site24x7.com/help/api/#credential-profile
resource "site24x7_credential_profile" "credential_profile_us" {
// (Required) Credential Profile Name.
credential_name = "Credential profile - terraform"
// (Required) Credential Profile Type.
credential_type = 3
// (Required) Username for the Credential Profile.
username = "Testing"
// (Required) Password for the Credential Profile.
password = "Test"
}
```

## Attributes Reference

### Required

* `credential_name` (String) Credential Profile Name.
* `credential_type` (Integer) Credential Profile Type.
* `username` (String) Username for the Credential Profile.
* `password` (String) Password for the Credential Profile.


Refer [API documentation](https://www.site24x7.com/help/api/#credential-profile) for more information about attributes.
Loading

0 comments on commit b9723db

Please sign in to comment.