Skip to content

Commit

Permalink
Added custom_device resource
Browse files Browse the repository at this point in the history
  • Loading branch information
vlad-preutu authored and Reinhard-Pilz-Dynatrace committed Jul 6, 2023
1 parent 76b2675 commit 04b516f
Show file tree
Hide file tree
Showing 8 changed files with 219 additions and 0 deletions.
110 changes: 110 additions & 0 deletions dynatrace/api/v2/customdevice/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/**
* @license
* Copyright 2020 Dynatrace LLC
*
* 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 customdevice

import (
"fmt"

"github.com/dynatrace-oss/terraform-provider-dynatrace/dynatrace/api"
customdevice "github.com/dynatrace-oss/terraform-provider-dynatrace/dynatrace/api/v2/customdevice/settings"
"github.com/dynatrace-oss/terraform-provider-dynatrace/dynatrace/rest"
"github.com/dynatrace-oss/terraform-provider-dynatrace/dynatrace/settings"
"github.com/google/uuid"
)

func Service(credentials *settings.Credentials) settings.CRUDService[*customdevice.CustomDevice] {
return &service{credentials}
}

type service struct {
credentials *settings.Credentials
}

func (me *service) Get(id string, v *customdevice.CustomDevice) error {
var err error

client := rest.DefaultClient(me.credentials.URL, me.credentials.Token)
entitySelector := `detectedName("` + id + `"),type("CUSTOM_DEVICE")`
req := client.Get(fmt.Sprintf("/api/v2/entities?from=now-3y&&entitySelector=%s", entitySelector)).Expect(200)
var enitityList customdevice.CustomDeviceList
if err = req.Finish(enitityList); err != nil {
return err
}

if len(enitityList.Entities) == 0 {
return rest.Error{Code: 404, Message: `Custom device with ID:` + id + " not found!"}
}

v.DisplayName = enitityList.Entities[0].DisplayName
v.EntityId = enitityList.Entities[0].EntityId
v.CustomDeviceID = id

return nil
}

func (me *service) SchemaID() string {
return "v2:environment:custom-device"
}

func (me *service) List() (api.Stubs, error) {
return api.Stubs{}, nil
}

func (me *service) Validate(v *customdevice.CustomDevice) error {
return nil // no endpoint for that
}

func (me *service) Create(v *customdevice.CustomDevice) (*api.Stub, error) {
var err error
if v.CustomDeviceID == "" {
v.CustomDeviceID = uuid.NewString()
}
resultDevice := customdevice.CustomDevice{}
client := rest.DefaultClient(me.credentials.URL, me.credentials.Token)
if err = client.Post("/api/v2/entities/custom", v, 201, 204).Finish(&resultDevice); err != nil {
return nil, err
}
resultDevice.CustomDeviceID = v.CustomDeviceID
resultDevice.DisplayName = v.DisplayName

return &api.Stub{ID: resultDevice.CustomDeviceID, Name: *resultDevice.DisplayName, Value: resultDevice}, nil
}

func (me *service) Update(id string, v *customdevice.CustomDevice) error {
var err error
v.CustomDeviceID = id
v.EntityId = nil
resultDevice := customdevice.CustomDevice{}
client := rest.DefaultClient(me.credentials.URL, me.credentials.Token)
if err = client.Post("/api/v2/entities/custom", v, 201, 204).Finish(&resultDevice); err != nil {
return err
}
return nil
}

func (me *service) Delete(id string) error {
return nil // no endpoint for that
}

func (me *service) New() *customdevice.CustomDevice {
return new(customdevice.CustomDevice)
}

func (me *service) Name() string {
return me.SchemaID()
}
33 changes: 33 additions & 0 deletions dynatrace/api/v2/customdevice/service_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* @license
* Copyright 2020 Dynatrace LLC
*
* 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 customdevice_test

import (
"testing"

"github.com/dynatrace-oss/terraform-provider-dynatrace/dynatrace/api/v2/customdevice"
"github.com/dynatrace-oss/terraform-provider-dynatrace/dynatrace/testing/api"
)

func TestCustomDevice(t *testing.T) {
api.TestService(t, customdevice.Service)
}

func TestAccCustomDevice(t *testing.T) {
api.TestAcc(t)
}
63 changes: 63 additions & 0 deletions dynatrace/api/v2/customdevice/settings/custom_device.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package customdevice

import (
"github.com/dynatrace-oss/terraform-provider-dynatrace/terraform/hcl"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

type CustomDevice struct {
EntityId *string `json:"entityId,omitempty"` // The ID of the custom device.
// Type *string `json:"type,omitempty"` // The type of the custom device.
DisplayName *string `json:"displayName,omitempty"` // The name of the custom device, displayed in the UI.
// Tags Tags `json:"tags,omitempty"` // A set of tags assigned to the custom device.
// Properties map[string]any `json:"properties"`
CustomDeviceID string `json:"customDeviceId,omitempty"`
}

type CustomDeviceList struct {
Entities []*CustomDevice `json:"entities"` // An unordered list of custom devices
}

func (me *CustomDevice) Schema() map[string]*schema.Schema {
return map[string]*schema.Schema{
"entity_id": {
Type: schema.TypeString,
Description: "The ID of the custom device.",
Computed: true,
},
"display_name": {
Type: schema.TypeString,
Description: "The name of the custom device, displayed in the UI.",
Required: true,
},
"custom_device_id": {
Type: schema.TypeString,
Description: "The unique name of the custom device.",
Optional: true,
Computed: true,
},
}
}

func (me *CustomDevice) MarshalHCL(properties hcl.Properties) error {
if err := properties.EncodeAll(map[string]any{
"entity_id": me.EntityId,
"display_name": me.DisplayName,
"custom_device_id": me.CustomDeviceID,
}); err != nil {
return err
}
return nil
}

func (me *CustomDevice) UnmarshalHCL(decoder hcl.Decoder) error {
return decoder.DecodeAll(map[string]any{
"entity_id": &me.EntityId,
"display_name": &me.DisplayName,
"custom_device_id": &me.CustomDeviceID,
})
}

func (me *CustomDevice) Name() string {
return *me.DisplayName
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"customDeviceId": "customDeviceId",
"displayName": "customDevicename"
}
4 changes: 4 additions & 0 deletions dynatrace/api/v2/customdevice/testdata/terraform/example_a.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
resource "dynatrace_custom_device" "#name#" {
custom_device_id = "customDeviceId"
display_name = "customDevicename"
}
2 changes: 2 additions & 0 deletions dynatrace/export/enums.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ var ResourceTypes = struct {
LimitOutboundConnections ResourceType
SpanEvents ResourceType
VMware ResourceType
CustomDevice ResourceType
}{
"dynatrace_autotag",
"dynatrace_autotag_v2",
Expand Down Expand Up @@ -519,6 +520,7 @@ var ResourceTypes = struct {
"dynatrace_limit_outbound_connections",
"dynatrace_span_events",
"dynatrace_vmware",
"dynatrace_custom_device",
}

func (me ResourceType) GetChildren() []ResourceType {
Expand Down
2 changes: 2 additions & 0 deletions dynatrace/export/resource_descriptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ import (
notificationsv1 "github.com/dynatrace-oss/terraform-provider-dynatrace/dynatrace/api/v1/config/notifications"
"github.com/dynatrace-oss/terraform-provider-dynatrace/dynatrace/api/v1/config/requestnaming/order"
locations "github.com/dynatrace-oss/terraform-provider-dynatrace/dynatrace/api/v1/config/synthetic/locations/private"
"github.com/dynatrace-oss/terraform-provider-dynatrace/dynatrace/api/v2/customdevice"
"github.com/dynatrace-oss/terraform-provider-dynatrace/dynatrace/api/v2/slo"
"github.com/dynatrace-oss/terraform-provider-dynatrace/dynatrace/settings"
"github.com/dynatrace-oss/terraform-provider-dynatrace/dynatrace/settings/services/cache"
Expand Down Expand Up @@ -968,6 +969,7 @@ var AllResources = map[ResourceType]ResourceDescriptor{
ResourceTypes.LimitOutboundConnections: NewResourceDescriptor(allowedoutboundconnections.Service),
ResourceTypes.SpanEvents: NewResourceDescriptor(eventattribute.Service),
ResourceTypes.VMware: NewResourceDescriptor(vmware.Service),
ResourceTypes.CustomDevice: NewResourceDescriptor(customdevice.Service),
}

var BlackListedResources = []ResourceType{
Expand Down
1 change: 1 addition & 0 deletions provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,7 @@ func Provider() *schema.Provider {
"dynatrace_web_app_key_performance_custom": resources.NewGeneric(export.ResourceTypes.WebAppKeyPerformanceCustom).Resource(),
"dynatrace_web_app_key_performance_load": resources.NewGeneric(export.ResourceTypes.WebAppKeyPerformanceLoad).Resource(),
"dynatrace_web_app_key_performance_xhr": resources.NewGeneric(export.ResourceTypes.WebAppKeyPerformanceXHR).Resource(),
"dynatrace_custom_device": resources.NewGeneric(export.ResourceTypes.CustomDevice).Resource(),
},
ConfigureContextFunc: config.ProviderConfigure,
}
Expand Down

0 comments on commit 04b516f

Please sign in to comment.