Working with securityPolicies in the Compute Engin

2019-08-27 06:18发布

I want to use the securityPolicies API for the Google Cloud Platform in Linux in a script written in Python.

To do this:

I installed google-api-python-client:

 pip install google-api-python-client

I generated and downloaded from GCP this credential information (private key) in JSON format and exported the path to this file under the environmental variable:

export GOOGLE_APPLICATION_CREDENTIALS='[PATH]'

Now I have a doubt about how to properly use the google-api-python-client library to achieve my goal.

Using the addRule method according to the documentation I write the following script (of course with valid values for project keys and securityPolicy), but when I execute it, although the interpreter doesn't return any error, but the script doesn't give the expected effect:

from googleapiclient import discovery
compute_service = discovery.build('compute', 'v1')
security_policies = compute_service.securityPolicies()
security_policies.addRule(
    project='existed_project_name',
    securityPolicy='existed_security_policy_name',
    body={
        'kind': 'compute#securityPolicyRule',
        'priority': 303,
        'action': 'deny(403)',
        'preview': False,
        'match': {
            'config': {
                'srcIpRanges': [
                    '192.0.2.0/24',
                    '198.51.100.0/24',
                    '203.0.113.0/24'
                ]
            },
            'versionedExpr': 'SRC_IPS_V1'
        }
    }
)

So I have the following questions:

1. What should I improve or change?

2. Is my approach to authentication correct?

Any ideas?

0条回答
登录 后发表回答