Skip to content

Commit

Permalink
Extensions Framework: Adds gRPC server implementations (Azure#4738)
Browse files Browse the repository at this point in the history
Adds gRPC server implementations
  • Loading branch information
wbreza authored Feb 5, 2025
1 parent 4acb164 commit 344b473
Show file tree
Hide file tree
Showing 27 changed files with 3,133 additions and 588 deletions.
1 change: 1 addition & 0 deletions cli/azd/.vscode/cspell-azd-dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ gosec
goterm
gotest
gotestsum
grpcserver
hotspot
ignorefile
iidfile
Expand Down
4 changes: 1 addition & 3 deletions cli/azd/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
PROTO_DIR=grpc/proto
GEN_DIR=pkg/azdext
INCLUDE_DIR=grpc/include # Local directory containing the protobuf include files

.PHONY: all proto clean

Expand All @@ -10,8 +9,7 @@ proto:
# Generate server-side code
mkdir -p $(GEN_DIR)
# Generate code for all .proto files into the same package
protoc --proto_path=$(INCLUDE_DIR) \
--proto_path=$(PROTO_DIR) \
protoc --proto_path=$(PROTO_DIR) \
--go_out=$(GEN_DIR) --go_opt=paths=source_relative \
--go-grpc_out=$(GEN_DIR) --go-grpc_opt=paths=source_relative \
$(PROTO_DIR)/*.proto
Expand Down
4 changes: 2 additions & 2 deletions cli/azd/cmd/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ func (m *monitorAction) Run(ctx context.Context) (*actions.ActionResult, error)
return nil, fmt.Errorf("discovering resource groups from deployment: %w", err)
}

var insightsResources []*azapi.Resource
var portalResources []*azapi.Resource
var insightsResources []*azapi.ResourceExtended
var portalResources []*azapi.ResourceExtended

for _, resourceGroup := range resourceGroups {
resources, err := m.resourceService.ListResourceGroupResources(
Expand Down
28 changes: 0 additions & 28 deletions cli/azd/grpc/proto/prompt.proto
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,6 @@ service PromptService {
rpc Select(SelectRequest) returns (SelectResponse);
rpc PromptSubscriptionResource(PromptSubscriptionResourceRequest) returns (PromptSubscriptionResourceResponse);
rpc PromptResourceGroupResource(PromptResourceGroupResourceRequest) returns (PromptResourceGroupResourceResponse);
rpc PromptStream(stream StreamWorkflowRequestMessage) returns (stream StreamWorkflowResponseMessage);
}

message StreamWorkflowRequestMessage{
oneof payload {
PromptOptions options = 1;
PromptValidation validation = 2;
}
}

message StreamWorkflowResponseMessage{
oneof payload {
PromptValue value = 1;
PromptFinalValue complete = 2;
}
}

message PromptValue {
string value = 1;
}

message PromptValidation {
bool valid = 1;
string message = 2;
}

message PromptFinalValue {
string value = 1;
}

message PromptSubscriptionRequest {}
Expand Down
148 changes: 148 additions & 0 deletions cli/azd/internal/grpcserver/deployment_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package grpcserver

import (
"context"

"github.com/azure/azure-dev/cli/azd/pkg/azapi"
"github.com/azure/azure-dev/cli/azd/pkg/azdext"
"github.com/azure/azure-dev/cli/azd/pkg/environment"
"github.com/azure/azure-dev/cli/azd/pkg/environment/azdcontext"
"github.com/azure/azure-dev/cli/azd/pkg/infra/provisioning/bicep"
"github.com/azure/azure-dev/cli/azd/pkg/lazy"
"github.com/azure/azure-dev/cli/azd/pkg/project"
)

type deploymentService struct {
azdext.UnimplementedDeploymentServiceServer

lazyAzdContext *lazy.Lazy[*azdcontext.AzdContext]
lazyEnvManager *lazy.Lazy[environment.Manager]
lazyProjectConfig *lazy.Lazy[*project.ProjectConfig]
lazyBicepProvider *lazy.Lazy[*bicep.BicepProvider]
deploymentService azapi.DeploymentService
}

func NewDeploymentService(
lazyAzdContext *lazy.Lazy[*azdcontext.AzdContext],
lazyEnvManager *lazy.Lazy[environment.Manager],
lazyProjectConfig *lazy.Lazy[*project.ProjectConfig],
lazyBicepProvider *lazy.Lazy[*bicep.BicepProvider],
azureDeploymentService azapi.DeploymentService,
) azdext.DeploymentServiceServer {
return &deploymentService{
lazyAzdContext: lazyAzdContext,
lazyEnvManager: lazyEnvManager,
lazyProjectConfig: lazyProjectConfig,
lazyBicepProvider: lazyBicepProvider,
deploymentService: azureDeploymentService,
}
}

func (s *deploymentService) GetDeployment(
ctx context.Context,
req *azdext.EmptyRequest,
) (*azdext.GetDeploymentResponse, error) {
azdContext, err := s.lazyAzdContext.GetValue()
if err != nil {
return nil, err
}

projectConfig, err := s.lazyProjectConfig.GetValue()
if err != nil {
return nil, err
}

bicepProvider, err := s.lazyBicepProvider.GetValue()
if err != nil {
return nil, err
}

if err := bicepProvider.Initialize(ctx, azdContext.ProjectDirectory(), projectConfig.Infra); err != nil {
return nil, err
}

latestDeployment, err := bicepProvider.LastDeployment(ctx)
if err != nil {
return nil, err
}

deployment := &azdext.Deployment{
Id: latestDeployment.Id,
Name: latestDeployment.Name,
Location: latestDeployment.Location,
DeploymentId: latestDeployment.DeploymentId,
Type: latestDeployment.Type,
Resources: []string{},
Tags: map[string]string{},
}

for key, value := range latestDeployment.Tags {
deployment.Tags[key] = *value
}

for _, resource := range latestDeployment.Resources {
deployment.Resources = append(deployment.Resources, *resource.ID)
}

return &azdext.GetDeploymentResponse{
Deployment: deployment,
}, nil
}

func (s *deploymentService) GetDeploymentContext(
ctx context.Context,
req *azdext.EmptyRequest,
) (*azdext.GetDeploymentContextResponse, error) {
azdContext, err := s.lazyAzdContext.GetValue()
if err != nil {
return nil, err
}

defaultEnvironment, err := azdContext.GetDefaultEnvironmentName()
if err != nil {
return nil, err
}

if defaultEnvironment == "" {
return nil, environment.ErrDefaultEnvironmentNotFound
}

envManager, err := s.lazyEnvManager.GetValue()
if err != nil {
return nil, err
}

env, err := envManager.Get(ctx, defaultEnvironment)
if err != nil {
return nil, err
}

tenantId := env.Getenv(environment.TenantIdEnvVarName)
subscriptionId := env.GetSubscriptionId()
resourceGroup := env.Getenv(environment.ResourceGroupEnvVarName)
location := env.Getenv(environment.LocationEnvVarName)

azureScope := &azdext.AzureScope{
TenantId: tenantId,
SubscriptionId: subscriptionId,
ResourceGroup: resourceGroup,
Location: location,
}

latestDeployment, err := s.GetDeployment(ctx, req)
if err != nil {
return nil, err
}

azureContext := &azdext.AzureContext{
Scope: azureScope,
Resources: latestDeployment.Deployment.Resources,
}

return &azdext.GetDeploymentContextResponse{
AzureContext: azureContext,
}, nil
}
Loading

0 comments on commit 344b473

Please sign in to comment.