Skip to content

Commit

Permalink
Fixed #297
Browse files Browse the repository at this point in the history
  • Loading branch information
Reinhard-Pilz-Dynatrace committed Jul 27, 2023
1 parent 978f4d4 commit a1ed6b9
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 18 deletions.
103 changes: 89 additions & 14 deletions dynatrace/api/v1/config/credentials/azure/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@ package azure

import (
"fmt"
"regexp"
"strings"
"sync"

"github.com/dynatrace-oss/terraform-provider-dynatrace/dynatrace/api"
azure "github.com/dynatrace-oss/terraform-provider-dynatrace/dynatrace/api/v1/config/credentials/azure/settings"
"github.com/dynatrace-oss/terraform-provider-dynatrace/dynatrace/rest"
"github.com/dynatrace-oss/terraform-provider-dynatrace/dynatrace/settings"
"github.com/dynatrace-oss/terraform-provider-dynatrace/dynatrace/settings/services/httpcache"
)

const SchemaID = "v1:config:credentials:azure"
Expand All @@ -32,22 +36,93 @@ const BasePath = "/api/config/v1/azure/credentials"
var mu sync.Mutex

func Service(credentials *settings.Credentials) settings.CRUDService[*azure.AzureCredentials] {
return settings.NewCRUDService(
return &service{service: settings.NewCRUDService(
credentials,
SchemaID,
settings.DefaultServiceOptions[*azure.AzureCredentials](BasePath).
WithMutex(mu.Lock, mu.Unlock).
WithOnBeforeUpdate(func(id string, v *azure.AzureCredentials) error {
if v.SupportingServicesManagedInDynatrace {
var creds azure.AzureCredentials
client := rest.DefaultClient(credentials.URL, credentials.Token)

if err := client.Get(fmt.Sprintf("%s/%s", BasePath, "id"), 200).Finish(&creds); err != nil {
return err
}
v.SupportingServices = creds.SupportingServices
WithMutex(mu.Lock, mu.Unlock),
), client: httpcache.DefaultClient(credentials.URL, credentials.Token, SchemaID)}
}

type service struct {
service settings.CRUDService[*azure.AzureCredentials]
client rest.Client
}

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

func (me *service) Get(id string, v *azure.AzureCredentials) error {
return me.service.Get(id, v)
}

func (me *service) SchemaID() string {
return me.service.SchemaID()
}

func (me *service) Create(v *azure.AzureCredentials) (*api.Stub, error) {
return me.service.Create(v)
}

var updateRegex = regexp.MustCompile("Invalid generic services configuration: You can't set up (.*) services using this endpoint")

type blackList []string

func (me blackList) Allows(v *azure.AzureSupportingService) bool {
if len(me) == 0 {
return true
}
for _, elem := range me {
if v.Name != nil && strings.TrimSpace(elem) == strings.TrimSpace(*v.Name) {
return false
}
}
return true
}

func (me *service) Update(id string, v *azure.AzureCredentials) error {
if v.SupportingServicesManagedInDynatrace {
var creds azure.AzureCredentials
if err := me.client.Get(fmt.Sprintf("%s/%s", BasePath, id), 200).Finish(&creds); err != nil {
return err
}
v.SupportingServices = creds.SupportingServices
}
if err := me.service.Update(id, v); err != nil {
if matches := updateRegex.FindStringSubmatch(err.Error()); len(matches) > 1 {
if len(v.SupportingServices) == 0 {
return err
}
var serviceStr string
if serviceStr = matches[1]; len(serviceStr) == 0 {
return err
}
blacklist := blackList{}
for _, service := range strings.Split(serviceStr, ",") {
blacklist = append(blacklist, strings.TrimSpace(service))
}
supportingServices := []*azure.AzureSupportingService{}
for _, supportingservice := range v.SupportingServices {
if blacklist.Allows(supportingservice) {
supportingServices = append(supportingServices, supportingservice)
}
return nil
}),
)
}
if len(v.SupportingServices) == len(supportingServices) {
return err
}
v.SupportingServices = supportingServices
return me.service.Update(id, v)
}
return err
}
return nil
}

func (me *service) Delete(id string) error {
return me.service.Delete(id)
}

func (me *service) Name() string {
return me.service.Name()
}
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,14 @@ func (ac *AzureCredentials) MarshalJSON() ([]byte, error) {
m["monitorOnlyExcludingTagPairs"] = rawMessage
}

if ac.SupportingServices != nil {
rawMessage, err := json.Marshal(ac.SupportingServices)
if err != nil {
return nil, err
}
m["supportingServices"] = rawMessage
}

if rawMessage, err := json.Marshal(opt.Bool(ac.Active)); err == nil {
m["active"] = rawMessage
} else {
Expand Down Expand Up @@ -246,7 +254,11 @@ func (ac *AzureCredentials) UnmarshalJSON(data []byte) error {
return err
}
}

if v, found := m["supportingServices"]; found {
if err := json.Unmarshal(v, &ac.SupportingServices); err != nil {
return err
}
}
if v, found := m["active"]; found {
if err := json.Unmarshal(v, &ac.Active); err != nil {
return err
Expand Down Expand Up @@ -326,9 +338,6 @@ func (ac *AzureCredentials) MarshalHCL(properties hcl.Properties) error {
if err := properties.Encode("monitor_only_tagged_entities", opt.Bool(ac.MonitorOnlyTaggedEntities)); err != nil {
return err
}
// if err := properties.Encode("supporting_services", ac.SupportingServices); err != nil {
// return err
// }
return nil
}

Expand Down

0 comments on commit a1ed6b9

Please sign in to comment.