Search code examples
pythonazureazure-sdkazure-sdk-python

Azure API Permission Fix


When trying to create a security group through the azure python sdk, I get this permissions issue: msrest.exceptions.ValidationError: Parameter 'SecurityRule.access' can not be None. How should I fix this permissions issue through the azure web console?


Solution

  • According to my understanding, you want to use python sdk to create an Azure Network security group. You can use the following script:

        from azure.common.credentials import ServicePrincipalCredentials
        from azure.mgmt.compute import ComputeManagementClient
        from azure.mgmt.network import NetworkManagementClient
        from azure.mgmt.network.v2017_03_01.models import NetworkSecurityGroup
        from azure.mgmt.network.v2017_03_01.models import SecurityRule
        from azure.mgmt.resource.resources import ResourceManagementClient
    
        subscription_id = 'xxxxxxxxx-xxxxxxxxxxxxxxxxxxxx'
        credentials = ServicePrincipalCredentials(
            client_id = 'xxxxxx-xxxx-xxx-xxxx-xxxxxxx',
            secret = 'xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx',
            tenant = 'xxxxxx-xxxxxxx'
        )
    
        network_client = NetworkManagementClient(
            credentials,
            subscription_id
        )
    
        resource_client = ResourceManagementClient(
            credentials,
            subscription_id
        )
    
        resource_client.providers.register('Microsoft.Network')
    
        resource_group_name = 'test-rg'
    
    
        async_security_rule = network_client.security_rules.create_or_update(
        resource_group_name,
        security_group_name,
        new_security_rule_name,
        {
                'access':azure.mgmt.network.v2017_03_01.models.SecurityRuleAccess.allow,
                'description':'New Test security rule',
                'destination_address_prefix':'*',
                'destination_port_range':'123-3500',
                'direction':azure.mgmt.network.v2017_03_01.models.SecurityRuleDirection.inbound,
                'priority':400,
                'protocol':azure.mgmt.network.v2017_03_01.models.SecurityRuleProtocol.tcp,
                'source_address_prefix':'*',
                'source_port_range':'655',
        }
    )
    
    security_rule = async_security_rule.result()
    

    For more details, please refer to the link