Skip to content

Commit

Permalink
Merge branch 'main' into willdavsmith/readd-ghcr
Browse files Browse the repository at this point in the history
  • Loading branch information
willdavsmith committed Apr 9, 2024
2 parents 3296758 + 74a3f25 commit 2b4c56d
Show file tree
Hide file tree
Showing 88 changed files with 739 additions and 381 deletions.
4 changes: 2 additions & 2 deletions pkg/armrpc/asyncoperation/worker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func (w *AsyncRequestProcessWorker) Start(ctx context.Context) error {
// 1. The same message is delivered twice in multiple instances.
// 2. provisioningState is not matched between resource and operationStatuses

dup, err := w.isDuplicated(reqCtx, asyncCtrl.StorageClient(), op.ResourceID, op.OperationID)
dup, err := w.isDuplicated(reqCtx, op.ResourceID, op.OperationID)
if err != nil {
opLogger.Error(err, "failed to check potential deduplication.")
return
Expand Down Expand Up @@ -370,7 +370,7 @@ func (w *AsyncRequestProcessWorker) updateResourceAndOperationStatus(ctx context
return nil
}

func (w *AsyncRequestProcessWorker) isDuplicated(ctx context.Context, sc store.StorageClient, resourceID string, operationID uuid.UUID) (bool, error) {
func (w *AsyncRequestProcessWorker) isDuplicated(ctx context.Context, resourceID string, operationID uuid.UUID) (bool, error) {
rID, err := resources.ParseResource(resourceID)
if err != nil {
return false, err
Expand Down
6 changes: 2 additions & 4 deletions pkg/armrpc/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,10 @@ type Builder struct {
// defaultHandlerOptions returns HandlerOption for the default operations such as getting operationStatuses and
// operationResults.
func defaultHandlerOptions(
ctx context.Context,
rootRouter chi.Router,
rootScopePath string,
namespace string,
availableOperations []v1.Operation,
ctrlOpts apictrl.Options) []server.HandlerOptions {
availableOperations []v1.Operation) []server.HandlerOptions {
namespace = strings.ToLower(namespace)

handlers := []server.HandlerOptions{}
Expand Down Expand Up @@ -122,7 +120,7 @@ func (b *Builder) ApplyAPIHandlers(ctx context.Context, r chi.Router, ctrlOpts a
rootScopePath := ctrlOpts.PathBase + UCPRootScopePath

// Configure the default handlers.
handlerOptions := defaultHandlerOptions(ctx, r, rootScopePath, b.namespaceNode.Name, b.namespaceNode.availableOperations, ctrlOpts)
handlerOptions := defaultHandlerOptions(r, rootScopePath, b.namespaceNode.Name, b.namespaceNode.availableOperations)

routerMap := map[string]chi.Router{}
for _, h := range b.registrations {
Expand Down
3 changes: 0 additions & 3 deletions pkg/azure/roleassignment/roleassignment.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,6 @@ func Create(ctx context.Context, armConfig *armauth.ArmConfig, subscriptionID, p
pager := client.NewListForScopePager(scope, &armauthorization.RoleAssignmentsClientListForScopeOptions{
Filter: &requestFilter,
})
if err != nil {
return nil, fmt.Errorf("failed to list role assignments: %w", err)
}

for pager.More() {
nextPage, err := pager.NextPage(ctx)
Expand Down
9 changes: 5 additions & 4 deletions pkg/cli/bicep/envinject.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@ func InjectApplicationParam(deploymentTemplate map[string]any, parameters map[st
}

func injectParam(deploymentTemplate map[string]any, parameters map[string]map[string]any, parameter string, value string) error {
if deploymentTemplate["parameters"] == nil {
return nil
formalParams, err := ExtractParameters(deploymentTemplate)
if err != nil {
return err
}

innerParameters := deploymentTemplate["parameters"].(map[string]any)
if innerParameters[parameter] == nil {
if _, ok := formalParams[parameter]; !ok {
// If we got here, it means 'parameter' is not a formal parameter of the template.
return nil
}

Expand Down
48 changes: 48 additions & 0 deletions pkg/cli/bicep/parameters.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
Copyright 2023 The Radius Authors.
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 bicep

import "fmt"

// ExtractParameters extracts the parameters from the deployment template.
func ExtractParameters(template map[string]any) (map[string]any, error) {
if template["parameters"] == nil {
return map[string]any{}, nil
}

params, ok := template["parameters"].(map[string]any)
if !ok {
return nil, fmt.Errorf("invalid template: parameters must be a map of maps, got: %T", template["parameters"])
}

return params, nil
}

// DefaultValue returns the default value of a parameter and a boolean indicating if it was found.
func DefaultValue(parameter any) (any, bool) {
if parameter == nil {
return nil, false
}

param, ok := parameter.(map[string]any)
if !ok {
return nil, false
}

defaultValue, ok := param["defaultValue"]
return defaultValue, ok
}
81 changes: 81 additions & 0 deletions pkg/cli/bicep/parameters_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
Copyright 2023 The Radius Authors.
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 bicep

import (
"encoding/json"
"os"
"testing"

"github.com/stretchr/testify/require"
)

func Test_ExtractParameters(t *testing.T) {
b, err := os.ReadFile("testdata/test-extractparameters.json")
require.NoError(t, err)

template := map[string]any{}
err = json.Unmarshal(b, &template)
require.NoError(t, err)

params, err := ExtractParameters(template)
require.NoError(t, err)

expected := map[string]any{
"application": map[string]any{
"metadata": map[string]any{
"description": "Specifies the application for resources.",
},
"type": "string",
},
"location": map[string]any{
"defaultValue": "westus2",
"metadata": map[string]any{
"description": "Specifies the location for resources.",
},
"type": "string",
},
}
require.Equal(t, expected, params)
}

func Test_DefaultValue_HasDefaultValue(t *testing.T) {
parameter := map[string]any{
"defaultValue": "westus2",
"metadata": map[string]any{
"description": "Specifies the location for resources.",
},
"type": "string",
}

value, ok := DefaultValue(parameter)
require.Equal(t, "westus2", value)
require.True(t, ok)
}

func Test_DefaultValue_NoDefaultValue(t *testing.T) {
parameter := map[string]any{
"metadata": map[string]any{
"description": "Specifies the location for resources.",
},
"type": "string",
}

value, ok := DefaultValue(parameter)
require.Nil(t, value)
require.False(t, ok)
}
24 changes: 24 additions & 0 deletions pkg/cli/bicep/testdata/test-extractparameters.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"languageVersion": "1.9-experimental",
"contentVersion": "1.0.0.0",
"metadata": {
},
"parameters": {
"location": {
"type": "string",
"defaultValue": "westus2",
"metadata": {
"description": "Specifies the location for resources."
}
},
"application": {
"type": "string",
"metadata": {
"description": "Specifies the application for resources."
}
}
},
"resources": {
}
}
8 changes: 4 additions & 4 deletions pkg/cli/clients/management.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (amc *UCPApplicationsManagementClient) ListAllResourcesOfTypeInApplication(
return nil, err
}
for _, resource := range resourceList {
isResourceWithApplication := isResourceInApplication(ctx, resource, applicationName)
isResourceWithApplication := isResourceInApplication(resource, applicationName)
if isResourceWithApplication {
results = append(results, resource)
}
Expand Down Expand Up @@ -162,7 +162,7 @@ func (amc *UCPApplicationsManagementClient) ListAllResourcesOfTypeInEnvironment(
return nil, err
}
for _, resource := range resourceList {
isResourceWithApplication := isResourceInEnvironment(ctx, resource, environmentName)
isResourceWithApplication := isResourceInEnvironment(resource, environmentName)
if isResourceWithApplication {
results = append(results, resource)
}
Expand Down Expand Up @@ -393,7 +393,7 @@ func (amc *UCPApplicationsManagementClient) CreateEnvironment(ctx context.Contex

}

func isResourceInApplication(ctx context.Context, resource generated.GenericResource, applicationName string) bool {
func isResourceInApplication(resource generated.GenericResource, applicationName string) bool {
obj, found := resource.Properties["application"]
// A resource may not have an application associated with it.
if !found {
Expand All @@ -417,7 +417,7 @@ func isResourceInApplication(ctx context.Context, resource generated.GenericReso
return false
}

func isResourceInEnvironment(ctx context.Context, resource generated.GenericResource, environmentName string) bool {
func isResourceInEnvironment(resource generated.GenericResource, environmentName string) bool {
obj, found := resource.Properties["environment"]
// A resource may not have an environment associated with it.
if !found {
Expand Down
4 changes: 2 additions & 2 deletions pkg/cli/cmd/bicep/publish/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func (r *Runner) publish(ctx context.Context) error {
}

// Prepare Destination
dst, err := r.prepareDestination(ctx)
dst, err := r.prepareDestination()
if err != nil {
return err
}
Expand Down Expand Up @@ -264,7 +264,7 @@ func (r *Runner) prepareSource(ctx context.Context) (*memory.Store, error) {
return src, nil
}

func (r *Runner) prepareDestination(ctx context.Context) (*remote.Repository, error) {
func (r *Runner) prepareDestination() (*remote.Repository, error) {
// Create a new credential store from Docker to get local credentials
ds, err := credentials.NewStoreFromDocker(credentials.StoreOptions{
AllowPlaintextPut: true,
Expand Down
4 changes: 2 additions & 2 deletions pkg/cli/cmd/bicep/publish/publish_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ func TestRunner_prepareDestination(t *testing.T) {
Destination: tt.dest,
PlainHTTP: tt.plainHTTP,
}
got, err := r.prepareDestination(context.Background())
got, err := r.prepareDestination()
if (err != nil) != tt.wantErr {
t.Errorf("Runner.prepareDestination() error = %v, wantErr %v", err, tt.wantErr)
return
Expand Down Expand Up @@ -318,7 +318,7 @@ func TestRunner_Validate(t *testing.T) {
radcli.SharedValidateValidation(t, NewCommand, tests)
}

func getError(registerUrl string, statusCode int) *errcode.ErrorResponse {
func getError(registerUrl string, statusCode int) *errcode.ErrorResponse {

err := &errcode.ErrorResponse{
URL: &url.URL{Host: registerUrl},
Expand Down
Loading

0 comments on commit 2b4c56d

Please sign in to comment.