示例#1
0
        public static TypeSymbol CreateModuleType(IEnumerable <TypeProperty> paramsProperties, IEnumerable <TypeProperty> outputProperties, ResourceScope moduleScope, ResourceScope containingScope, string typeName)
        {
            var paramsType = new NamedObjectType(ModuleParamsPropertyName, TypeSymbolValidationFlags.Default, paramsProperties, null);
            // If none of the params are reqired, we can allow the 'params' declaration to be ommitted entirely
            var paramsRequiredFlag = paramsProperties.Any(x => x.Flags.HasFlag(TypePropertyFlags.Required)) ? TypePropertyFlags.Required : TypePropertyFlags.None;

            var outputsType = new NamedObjectType(ModuleOutputsPropertyName, TypeSymbolValidationFlags.Default, outputProperties, null);

            var scopePropertyFlags = TypePropertyFlags.WriteOnly | TypePropertyFlags.DeployTimeConstant;

            if (moduleScope != containingScope)
            {
                // If the module scope matches the parent scope, we can safely omit the scope property
                scopePropertyFlags |= TypePropertyFlags.Required;
            }

            var moduleBody = new NamedObjectType(
                typeName,
                TypeSymbolValidationFlags.Default,
                new[]
            {
                new TypeProperty(ResourceNamePropertyName, LanguageConstants.String, TypePropertyFlags.Required | TypePropertyFlags.DeployTimeConstant),
                new TypeProperty(ResourceScopePropertyName, CreateResourceScopeReference(moduleScope), scopePropertyFlags),
                new TypeProperty(ModuleParamsPropertyName, paramsType, paramsRequiredFlag | TypePropertyFlags.WriteOnly),
                new TypeProperty(ModuleOutputsPropertyName, outputsType, TypePropertyFlags.ReadOnly),
                new TypeProperty(ResourceDependsOnPropertyName, ResourceOrResourceCollectionRefArray, TypePropertyFlags.WriteOnly),
            },
                null);

            return(new ModuleType(typeName, moduleScope, moduleBody));
        }
示例#2
0
        public ResourceType GetType(ResourceTypeReference reference, bool isExistingResource)
        {
            var bodyType     = new NamedObjectType(reference.FormatName(), TypeSymbolValidationFlags.Default, LanguageConstants.CreateResourceProperties(reference), null);
            var resourceType = new ResourceType(reference, ResourceScope.Tenant | ResourceScope.ManagementGroup | ResourceScope.Subscription | ResourceScope.ResourceGroup | ResourceScope.Resource, bodyType);

            return(AzResourceTypeProvider.SetBicepResourceProperties(resourceType, isExistingResource));
        }
        public void DiscriminatedObject_should_throw_for_various_badly_formatted_object_arguments()
        {
            var namedObjectA = new NamedObjectType("objA", new []
            {
                new TypeProperty("discKey", new StringLiteralType("keyA")),
                new TypeProperty("keyAProp", LanguageConstants.String),
            }, null);

            var missingKeyObject = new NamedObjectType("objB", new []
            {
                new TypeProperty("keyBProp", LanguageConstants.String),
            }, null);
            Action missingKeyConstructorAction = () => new DiscriminatedObjectType("discObj", "discKey", new [] { namedObjectA, missingKeyObject });

            missingKeyConstructorAction.Should().Throw <ArgumentException>();

            var invalidKeyTypeObject = new NamedObjectType("objB", new []
            {
                new TypeProperty("discKey", LanguageConstants.String),
                new TypeProperty("keyBProp", LanguageConstants.String),
            }, null);
            Action invalidKeyTypeConstructorAction = () => new DiscriminatedObjectType("discObj", "discKey", new [] { namedObjectA, invalidKeyTypeObject });

            invalidKeyTypeConstructorAction.Should().Throw <ArgumentException>();

            var duplicateKeyObject = new NamedObjectType("objB", new []
            {
                new TypeProperty("discKey", new StringLiteralType("keyA")),
                new TypeProperty("keyBProp", LanguageConstants.String),
            }, null);
            Action duplicateKeyConstructorAction = () => new DiscriminatedObjectType("discObj", "discKey", new [] { namedObjectA, duplicateKeyObject });

            duplicateKeyConstructorAction.Should().Throw <ArgumentException>();
        }
        public static ResourceType CreateCustomResourceType(string fullyQualifiedType, string apiVersion, TypeSymbolValidationFlags validationFlags, params TypeProperty[] customProperties)
        {
            var reference = ResourceTypeReference.Parse($"{fullyQualifiedType}@{apiVersion}");

            var resourceProperties = LanguageConstants.GetCommonResourceProperties(reference)
                                     .Concat(new TypeProperty("properties", new NamedObjectType("properties", validationFlags, customProperties, null), TypePropertyFlags.Required));

            var bodyType = new NamedObjectType(reference.FormatName(), validationFlags, resourceProperties, null);

            return(new ResourceType(reference, ResourceScope.Tenant | ResourceScope.ManagementGroup | ResourceScope.Subscription | ResourceScope.ResourceGroup | ResourceScope.Resource, bodyType));
        }
示例#5
0
        private ResourceType GenerateResourceType(ResourceTypeReference typeReference, bool isExistingResource)
        {
            if (availableResourceTypes.TryGetValue(typeReference, out var typeLocation))
            {
                var serializedResourceType = typeLoader.LoadResourceType(typeLocation);
                var resourceType           = resourceTypeFactory.GetResourceType(serializedResourceType);

                return(SetBicepResourceProperties(resourceType, isExistingResource));
            }
            else
            {
                var resourceBodyType = new NamedObjectType(typeReference.FormatName(), TypeSymbolValidationFlags.Default, LanguageConstants.CreateResourceProperties(typeReference), null);
                var resourceType     = new ResourceType(typeReference, ResourceScope.Tenant | ResourceScope.ManagementGroup | ResourceScope.Subscription | ResourceScope.ResourceGroup | ResourceScope.Resource, resourceBodyType);

                return(SetBicepResourceProperties(resourceType, isExistingResource));
            }
        }
示例#6
0
        private static ObjectType GetResourceGroupReturnValue(IEnumerable <FunctionArgumentSyntax> arguments)
        {
            var properties = new NamedObjectType("properties", TypeSymbolValidationFlags.Default, new []
            {
                new TypeProperty("provisioningState", LanguageConstants.String),
            }, null);

            return(new ResourceGroupScopeType(arguments, new []
            {
                new TypeProperty("id", LanguageConstants.String),
                new TypeProperty("name", LanguageConstants.String),
                new TypeProperty("type", LanguageConstants.String),
                new TypeProperty("location", LanguageConstants.String),
                new TypeProperty("managedBy", LanguageConstants.String),
                new TypeProperty("tags", LanguageConstants.Tags),
                new TypeProperty("properties", properties),
            }));
        }
示例#7
0
        public static TypeSymbol CreateModuleType(IEnumerable <TypeProperty> paramsProperties, IEnumerable <TypeProperty> outputProperties, string typeName)
        {
            var paramsType = new NamedObjectType(ModuleParamsPropertyName, TypeSymbolValidationFlags.Default, paramsProperties, null);
            // If none of the params are reqired, we can allow the 'params' declaration to be ommitted entirely
            var paramsRequiredFlag = paramsProperties.Any(x => x.Flags.HasFlag(TypePropertyFlags.Required)) ? TypePropertyFlags.Required : TypePropertyFlags.None;

            var outputsType = new NamedObjectType(ModuleOutputsPropertyName, TypeSymbolValidationFlags.Default, outputProperties, null);

            return(new NamedObjectType(
                       typeName,
                       TypeSymbolValidationFlags.Default,
                       new []
            {
                new TypeProperty(ModuleNamePropertyName, LanguageConstants.String, TypePropertyFlags.Required | TypePropertyFlags.SkipInlining),
                new TypeProperty(ModuleParamsPropertyName, paramsType, paramsRequiredFlag | TypePropertyFlags.WriteOnly),
                new TypeProperty(ModuleOutputsPropertyName, outputsType, TypePropertyFlags.ReadOnly),
            },
                       null));
        }
        public void DiscriminatedObjectType_should_be_correctly_instantiated()
        {
            var namedObjectA = new NamedObjectType("objA", new []
            {
                new TypeProperty("discKey", new StringLiteralType("keyA")),
                new TypeProperty("keyAProp", LanguageConstants.String),
            }, null);

            var namedObjectB = new NamedObjectType("objB", new []
            {
                new TypeProperty("discKey", new StringLiteralType("keyB")),
                new TypeProperty("keyBProp", LanguageConstants.String),
            }, null);

            var discObj = new DiscriminatedObjectType("discObj", "discKey", new [] { namedObjectA, namedObjectB });

            discObj.UnionMembersByKey.Keys.Should().BeEquivalentTo("'keyA'", "'keyB'");
            discObj.TypeKind.Should().Be(TypeKind.DiscriminatedObject);

            discObj.UnionMembersByKey[new StringLiteralType("keyA").Name].Type.Should().Be(namedObjectA);
            discObj.UnionMembersByKey[new StringLiteralType("keyB").Name].Type.Should().Be(namedObjectB);
        }
示例#9
0
        public ResourceType GetType(ResourceScope scopeType, ResourceTypeReference typeReference)
        {
            if (loadedTypeCache.TryGetValue(typeReference, out var resourceType))
            {
                return(resourceType);
            }

            if (availableResourceTypes[scopeType].TryGetValue(typeReference, out var typeLocation))
            {
                // It's important to cache this result because LoadResourceType is an expensive operation, and
                // duplicating types means the resourceTypeFactor won't be able to use its cache.
                var serializedResourceType = typeLoader.LoadResourceType(typeLocation);
                resourceType = resourceTypeFactory.GetResourceType(serializedResourceType);
            }
            else
            {
                var resourceBodyType = new NamedObjectType(typeReference.FormatName(), TypeSymbolValidationFlags.Default, LanguageConstants.CreateResourceProperties(typeReference), null);
                resourceType = new ResourceType(typeReference, resourceBodyType);
            }

            loadedTypeCache[typeReference] = resourceType;
            return(resourceType);
        }
示例#10
0
        private static (ResourceTypeReference, Func <ResourceType>) Get_Microsoft_Resources_deploymentScripts_2020_10_01()
        {
            // hand crafted from https://github.com/Azure/azure-rest-api-specs/blob/405df4e/specification/resources/resource-manager/Microsoft.Resources/preview/2019-10-01-preview/deploymentScripts.json
            var reference = ResourceTypeReference.Parse("Microsoft.Resources/deploymentScripts@2019-10-01-preview");

            var userAssignedIdentity = new NamedObjectType(
                "UserAssignedIdentity",
                TypeSymbolValidationFlags.WarnOnTypeMismatch,
                new [] {
                new TypeProperty("clientId", LanguageConstants.String),
                new TypeProperty("principalId", LanguageConstants.String),
            },
                null);

            var identity = new NamedObjectType(
                "ManagedServiceIdentity",
                TypeSymbolValidationFlags.WarnOnTypeMismatch,
                new [] {
                new TypeProperty("type", CreateEnumType("UserAssigned"), TypePropertyFlags.None),
                new TypeProperty("userAssignedIdentities", new NamedObjectType("UserAssignedIdentities", TypeSymbolValidationFlags.WarnOnTypeMismatch, Enumerable.Empty <TypeProperty>(), userAssignedIdentity), TypePropertyFlags.None),
            },
                null);

            var containerConfiguration = new NamedObjectType(
                "ContainerConfiguration",
                TypeSymbolValidationFlags.WarnOnTypeMismatch,
                new [] {
                new TypeProperty("containerGroupName", LanguageConstants.String, TypePropertyFlags.None),
            },
                null);

            var environmentVariable = new NamedObjectType(
                "EnvironmentVariable",
                TypeSymbolValidationFlags.WarnOnTypeMismatch,
                new [] {
                new TypeProperty("name", LanguageConstants.String, TypePropertyFlags.Required),
                new TypeProperty("secureValue", LanguageConstants.String, TypePropertyFlags.None),
                new TypeProperty("value", LanguageConstants.String, TypePropertyFlags.None),
            },
                null);

            var storageAccountConfiguration = new NamedObjectType(
                "StorageAccountConfiguration",
                TypeSymbolValidationFlags.WarnOnTypeMismatch,
                new [] {
                new TypeProperty("storageAccountKey", LanguageConstants.String, TypePropertyFlags.None),
                new TypeProperty("storageAccountName", LanguageConstants.String, TypePropertyFlags.None),
            },
                null);

            var scriptStatus = new NamedObjectType(
                "ScriptStatus",
                TypeSymbolValidationFlags.WarnOnTypeMismatch,
                new [] {
                new TypeProperty("containerInstanceId", LanguageConstants.String, TypePropertyFlags.ReadOnly),
                new TypeProperty("storageAccountId", LanguageConstants.String, TypePropertyFlags.ReadOnly),
                new TypeProperty("startTime", LanguageConstants.String, TypePropertyFlags.ReadOnly),
                new TypeProperty("endTime", LanguageConstants.String, TypePropertyFlags.ReadOnly),
                new TypeProperty("expirationTime", LanguageConstants.String, TypePropertyFlags.ReadOnly),
                new TypeProperty("error", LanguageConstants.Any, TypePropertyFlags.ReadOnly),
            },
                null);

            var baseResource = LanguageConstants.GetCommonResourceProperties(reference).Concat(
                new TypeProperty("location", LanguageConstants.String, TypePropertyFlags.Required),
                new TypeProperty("tags", LanguageConstants.Tags, TypePropertyFlags.None),
                new TypeProperty("identity", identity, TypePropertyFlags.Required));

            var deploymentScriptPropertiesBase = new [] {
                new TypeProperty("containerSettings", containerConfiguration, TypePropertyFlags.None),
                new TypeProperty("storageAccountSettings", storageAccountConfiguration, TypePropertyFlags.None),
                new TypeProperty("cleanupPreference", UnionType.Create(new StringLiteralType("Always"), new StringLiteralType("OnSuccess"), new StringLiteralType("OnExpiration")), TypePropertyFlags.None),
                new TypeProperty("provisioningState", CreateEnumType("Creating", "ProvisioningResources", "Running", "Succeeded", "Failed", "Canceled"), TypePropertyFlags.ReadOnly),
                new TypeProperty("status", scriptStatus, TypePropertyFlags.ReadOnly),
                new TypeProperty("outputs", LanguageConstants.Any, TypePropertyFlags.ReadOnly),
            };

            var scriptConfigurationBase = new [] {
                new TypeProperty("primaryScriptUri", LanguageConstants.String, TypePropertyFlags.None),
                new TypeProperty("supportingScriptUris", new TypedArrayType(LanguageConstants.String, TypeSymbolValidationFlags.WarnOnTypeMismatch), TypePropertyFlags.None),
                new TypeProperty("scriptContent", LanguageConstants.String, TypePropertyFlags.None),
                new TypeProperty("arguments", LanguageConstants.String, TypePropertyFlags.None),
                new TypeProperty("environmentVariables", new TypedArrayType(environmentVariable, TypeSymbolValidationFlags.WarnOnTypeMismatch), TypePropertyFlags.None),
                new TypeProperty("forceUpdateTag", LanguageConstants.String, TypePropertyFlags.None),
                new TypeProperty("retentionInterval", LanguageConstants.String, TypePropertyFlags.Required),
                new TypeProperty("timeout", LanguageConstants.String, TypePropertyFlags.None),
            };

            var powerShellResourceProperties = new NamedObjectType(
                "AzurePowerShellScriptProperties",
                TypeSymbolValidationFlags.WarnOnTypeMismatch,
                deploymentScriptPropertiesBase.Concat(scriptConfigurationBase).Concat(
                    new TypeProperty("azPowerShellVersion", LanguageConstants.String, TypePropertyFlags.Required)
                    ),
                null);

            var powerShellResource = new NamedObjectType(
                "AzurePowerShellScript",
                TypeSymbolValidationFlags.WarnOnTypeMismatch,
                baseResource.Concat(
                    new TypeProperty("kind", new StringLiteralType("AzurePowerShell"), TypePropertyFlags.Required),
                    new TypeProperty("properties", powerShellResourceProperties, TypePropertyFlags.Required)),
                null);

            var azCliResourceProperties = new NamedObjectType(
                "AzureCliScriptProperties",
                TypeSymbolValidationFlags.WarnOnTypeMismatch,
                deploymentScriptPropertiesBase.Concat(scriptConfigurationBase).Concat(
                    new TypeProperty("azCliVersion", LanguageConstants.String, TypePropertyFlags.Required)
                    ),
                null);

            var azCliResource = new NamedObjectType(
                "AzureCliScript",
                TypeSymbolValidationFlags.WarnOnTypeMismatch,
                baseResource.Concat(
                    new TypeProperty("kind", new StringLiteralType("AzureCLI"), TypePropertyFlags.Required),
                    new TypeProperty("properties", azCliResourceProperties, TypePropertyFlags.Required)),
                null);

            return(reference, () => new ResourceType(
                       reference,
                       new DiscriminatedObjectType(
                           reference.FormatName(),
                           TypeSymbolValidationFlags.WarnOnTypeMismatch,
                           "kind",
                           new [] {
                powerShellResource,
                azCliResource,
            }),
                       TypeSymbolValidationFlags.WarnOnTypeMismatch));
        }