Skip to content

Commit

Permalink
importer-msgraph-metadata: add service workaround to add missing GET …
Browse files Browse the repository at this point in the history
…method for synchronization secrets
  • Loading branch information
manicminer committed Sep 5, 2024
1 parent ff34b34 commit 6cef487
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/hashicorp/pandora/tools/importer-msgraph-metadata/components/versions"
)

var _ workaround = workaroundApplication{}
var _ dataWorkaround = workaroundApplication{}

// workaroundApplication works around missing fields in the Application model for the beta API.
// 1. Missing `oauth2RequirePostResponse` field.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/hashicorp/pandora/tools/importer-msgraph-metadata/components/parser"
)

var _ workaround = workaroundConditionalAccessPolicy{}
var _ dataWorkaround = workaroundConditionalAccessPolicy{}

// workaroundConditionalAccessPolicy adds missing fields and fixes some field types.
type workaroundConditionalAccessPolicy struct{}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/hashicorp/pandora/tools/importer-msgraph-metadata/internal/logging"
)

var _ workaround = workaroundODataBind{}
var _ dataWorkaround = workaroundODataBind{}

// workaroundODataBind inserts an `@odata.bind` field where a field or collection refers to a DirectoryObject. The
// OpenAPI spec unfortunately does not document relationships between entities.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/hashicorp/pandora/tools/importer-msgraph-metadata/internal/logging"
)

var _ workaround = workaroundRepeatingResourceIdSegments{}
var _ dataWorkaround = workaroundRepeatingResourceIdSegments{}

// workaroundRepeatingResourceIdSegments removes incompatible resource IDs due to repeating segments which are not supported at this time.
type workaroundRepeatingResourceIdSegments struct{}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package workarounds

import (
"fmt"
"net/http"

"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/pandora/tools/importer-msgraph-metadata/components/normalize"
"github.com/hashicorp/pandora/tools/importer-msgraph-metadata/components/parser"
)

var _ serviceWorkaround = workaroundSynchronizationSecrets{}

// workaroundSynchronizationSecrets adds a missing GET method for synchronization secrets, which is absent from upstream specs.
type workaroundSynchronizationSecrets struct{}

func (workaroundSynchronizationSecrets) Name() string {
return "Synchronization Secrets / add missing get method"
}

func (workaroundSynchronizationSecrets) Process(apiVersion, serviceName string, resources parser.Resources, resourceIds parser.ResourceIds) error {
serviceNamesToPaths := map[string]string{
"servicePrincipals": "/servicePrincipals/{servicePrincipal-id}/synchronization/secrets",
}

for serviceNameToMatch, path := range serviceNamesToPaths {
if serviceNameToMatch != serviceName {
return nil
}

resourceName := fmt.Sprintf("%sSynchronizationSecret", normalize.Singularize(normalize.CleanName(serviceName)))
resource, ok := resources[resourceName]
if !ok {
return fmt.Errorf("%q was not found for the service %q", resourceName, serviceName)
}

tags := []string{serviceName + ".synchronization"}

var resourceId *parser.ResourceId
var uriSuffix *string

parsedPath := parser.NewResourceId(path, tags)
match, ok := resourceIds.MatchIdOrAncestor(parsedPath)
if ok {
if match.Id != nil {
resourceId = match.Id
}
if match.Remainder != nil && len(match.Remainder.Segments) > 0 {
uriSuffix = pointer.To(match.Remainder.ID())
}
} else {
uriSuffix = pointer.To(parsedPath.ID())
}

resource.Operations = append(resource.Operations, parser.Operation{
Name: "GetSynchronizationSecret",
Description: "Retrieve synchronization secrets.",
Type: parser.OperationTypeRead,
Method: http.MethodGet,
ResourceId: resourceId,
UriSuffix: uriSuffix,
Responses: parser.Responses{
{
Status: http.StatusOK,
ContentType: pointer.To("application/json"),
ReferenceName: pointer.To("microsoft.graph.synchronizationSecret"),
Type: pointer.To(parser.DataTypeReference),
},
},
Tags: tags,
})
}

return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,37 @@ import (
"github.com/hashicorp/pandora/tools/importer-msgraph-metadata/internal/logging"
)

var workarounds = []workaround{
var workarounds = []dataWorkaround{
workaroundODataBind{},
workaroundRepeatingResourceIdSegments{},

workaroundApplication{},
workaroundConditionalAccessPolicy{},
}

var serviceWorkarounds = []serviceWorkaround{
workaroundSynchronizationSecrets{},
}

type workaround interface {
// Name returns the Service Name and associated Pull Request number
Name() string
}

type dataWorkaround interface {
workaround

// Process performs any necessary fixes to constants, models and/or resource IDs
Process(string, parser.Models, parser.Constants, parser.ResourceIds) error
}

type serviceWorkaround interface {
workaround

// Process performs any necessary fixes to resources
Process(string, string, parser.Resources, parser.ResourceIds) error
}

// ApplyWorkarounds invokes the specified workarounds for models, constants and resource
func ApplyWorkarounds(apiVersion string, models parser.Models, constants parser.Constants, resourceIds parser.ResourceIds) error {
logging.Tracef("Processing Data Workarounds..")
Expand All @@ -38,3 +53,16 @@ func ApplyWorkarounds(apiVersion string, models parser.Models, constants parser.

return nil
}

// ApplyWorkaroundsForService invokes the specified workarounds for a given service
func ApplyWorkaroundsForService(apiVersion, serviceName string, resources parser.Resources, resourceIds parser.ResourceIds) error {
logging.Tracef("Processing Service Workarounds for %s..", serviceName)
for _, fix := range serviceWorkarounds {
logging.Tracef("Applying Service Workaround %q", fix.Name())
if err := fix.Process(apiVersion, serviceName, resources, resourceIds); err != nil {
return fmt.Errorf("applying Service Workaround %q: %v", fix.Name(), err)
}
}

return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,10 @@ func (p pipelineForService) RunImport() error {
return nil
}

p.resources[p.service] = resources
// Apply workarounds
if err = workarounds.ApplyWorkaroundsForService(p.apiVersion, p.service, resources, p.resourceIds); err != nil {
return err
}

// Consistency checks for discovered resources
for resourceName, resource := range resources {
Expand All @@ -177,6 +180,7 @@ func (p pipelineForService) RunImport() error {
}
}

p.resources[p.service] = resources
return nil
}

Expand Down

0 comments on commit 6cef487

Please sign in to comment.