-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
76b2675
commit 04b516f
Showing
8 changed files
with
219 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,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() | ||
} |
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,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) | ||
} |
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,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 | ||
} |
4 changes: 4 additions & 0 deletions
4
dynatrace/api/v2/customdevice/testdata/default-test-case/0001-create.json
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 @@ | ||
{ | ||
"customDeviceId": "customDeviceId", | ||
"displayName": "customDevicename" | ||
} |
4 changes: 4 additions & 0 deletions
4
dynatrace/api/v2/customdevice/testdata/terraform/example_a.tf
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 @@ | ||
resource "dynatrace_custom_device" "#name#" { | ||
custom_device_id = "customDeviceId" | ||
display_name = "customDevicename" | ||
} |
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
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