Example #1
0
 public ProvisionAzure(ISettings settings, IServicePrincipal principal, IAzureRestClient azureRestClient)
 {
     Principal       = principal;
     Settings        = settings;
     AzureRestClient = azureRestClient;
     CommonTags      = new Dictionary <string, string>();
 }
Example #2
0
 public VirtualMachineController(IAzureRestClient azureRestClient)
 {
     _azureRestClient = azureRestClient;
 }
Example #3
0
        public override void ExecuteCmdlet()
        {
            this.ValidateParameters();

            context = DefaultContext;
            AzureOperationResponse <string> response;

            if (ByParameters.Equals(this.ParameterSetName))
            {
                this.Path  = ConstructPath(this.IsParameterBound(c => c.SubscriptionId) ? this.SubscriptionId : context.Subscription.Id, this.ResourceGroupName, this.ResourceProviderName, this.ResourceType, this.Name);
                this.Path += $"?api-version={this.ApiVersion}";
            }
            else if (ByURI.Equals(this.ParameterSetName))
            {
                this.Path = Uri.PathAndQuery;
            }

            IAzureRestClient serviceClient = null;

            if (ByPath.Equals(this.ParameterSetName) || ByParameters.Equals(this.ParameterSetName))
            {
                serviceClient = AzureSession.Instance.ClientFactory.CreateArmClient <AzureRestClient>(context, AzureEnvironment.Endpoint.ResourceManager);
            }
            else if (ByURI.Equals(this.ParameterSetName))
            {
                string targetResourceIdKey = null;
                string resourceId          = this.IsParameterBound(c => c.ResourceId) ? ResourceId.ToString() : null;
                if (this.IsParameterBound(c => c.ResourceId) &&
                    context.Environment.ActiveDirectoryServiceEndpointResourceId.Equals(resourceId) &&
                    !HasSameEndpoint(Uri.Authority, context.Environment.ResourceManagerUrl))
                {
                    throw new AzPSArgumentException("The resource ID of Azure Resource Manager cannot be used for other endpoint. Please make sure to input the correct resource ID that matches the request URI.",
                                                    nameof(ResourceId));
                }
                var targetResourceId = string.IsNullOrEmpty(resourceId) ? MatchResourceId(context, Uri.Authority, out targetResourceIdKey) : resourceId;
                if (string.IsNullOrWhiteSpace(targetResourceId))
                {
                    throw new AzPSArgumentException("Cannot find resource id(audience) for authentication", nameof(ResourceId));
                }

                ServiceClientCredentials creds = null;
                if (AzureSession.Instance.AuthenticationFactory is Commands.Common.Authentication.Factories.AuthenticationFactory factory)
                {
                    creds = factory.GetServiceClientCredentials(context, targetResourceIdKey, targetResourceId);
                }
                else
                {
                    creds = AzureSession.Instance.AuthenticationFactory.GetServiceClientCredentials(context, targetResourceId);
                }
                Uri baseUri = new Uri($"{Uri.Scheme}://{Uri.Authority}");
                serviceClient = AzureSession.Instance.ClientFactory.CreateCustomArmClient <AzureRestClient>(baseUri, creds);
            }
            else
            {
                WriteErrorWithTimestamp("Parameter set is not implemented");
            }

            switch (this.Method.ToUpper())
            {
            case "GET":
                response = serviceClient
                           .Operations
                           .GetResourceWithFullResponse(this.Path, this.ApiVersion);
                break;

            case "POST":
                response = serviceClient
                           .Operations
                           .PostResourceWithFullResponse(this.Path, this.ApiVersion, this.Payload);
                break;

            case "PUT":
                response = serviceClient
                           .Operations
                           .PutResourceWithFullResponse(this.Path, this.ApiVersion, this.Payload);
                break;

            case "PATCH":
                response = serviceClient
                           .Operations
                           .PatchResourceWithFullResponse(this.Path, this.ApiVersion, this.Payload);
                break;

            case "DELETE":
                response = serviceClient
                           .Operations
                           .DeleteResourceWithFullResponse(this.Path, this.ApiVersion);
                break;

            default:
                throw new AzPSArgumentException("Invalid HTTP Method", nameof(Method));
            }

            WriteObject(new PSHttpResponse(response));
        }
Example #4
0
 public SiteExtension(IAzureRestClient client)
 {
     this.azureRestClient = client;
 }
Example #5
0
 public AppInsights(IAzureRestClient client)
 {
     this.azureRestClient = client;
 }