Openstack api调用安全组

调用security group

import requests
import json
headers={}
headers['Content-Type']='application/json'
headers['X-Auth-Token']=''

a={
    'security_group':{
        'name':'pvm_sec'
    }
}

a=requests.post(url='http://172.25.253.17:9696/v2.0/security-groups',data=json.dumps(a),headers=headers)
print(a.text)
print('安全组创建成功')

info=json.loads(a.text)
id=info['security_group']['id']
print('安全组id'+id)

b={
    'security_group_rule':{
        'direction':'ingress',
        'port_range_min':'20',
        'ethertype':'IPv4',
        'port_range_max':'3306',
        'protocol':'tcp',
        'security_group_id':id
    }
}

b=requests.post(url='http://172.25.253.17:9696/v2.0/security-group-rules',data=json.dumps(b),headers=headers)
print(b.text)
print('安全组规则添加成功')

# python3 create_security_group.py 
{"security_group": {"description": "", "tags": [], "tenant_id": "55b50cbb4dd4459b873cb15a8b03db43", "created_at": "2021-12-14T04:36:24Z", "updated_at": "2021-12-14T04:36:24Z", "security_group_rules": [{"direction": "egress", "protocol": null, "description": null, "tags": [], "port_range_max": null, "updated_at": "2021-12-14T04:36:24Z", "revision_number": 0, "id": "2db7c94f-f8be-43a1-b63d-006307e8d508", "remote_group_id": null, "remote_ip_prefix": null, "created_at": "2021-12-14T04:36:24Z", "security_group_id": "323f1461-781d-4f01-8636-190335019a5c", "tenant_id": "55b50cbb4dd4459b873cb15a8b03db43", "port_range_min": null, "ethertype": "IPv6", "project_id": "55b50cbb4dd4459b873cb15a8b03db43"}, {"direction": "egress", "protocol": null, "description": null, "tags": [], "port_range_max": null, "updated_at": "2021-12-14T04:36:24Z", "revision_number": 0, "id": "61955a6f-e76c-4568-a440-466de0bb59e2", "remote_group_id": null, "remote_ip_prefix": null, "created_at": "2021-12-14T04:36:24Z", "security_group_id": "323f1461-781d-4f01-8636-190335019a5c", "tenant_id": "55b50cbb4dd4459b873cb15a8b03db43", "port_range_min": null, "ethertype": "IPv4", "project_id": "55b50cbb4dd4459b873cb15a8b03db43"}], "revision_number": 2, "project_id": "55b50cbb4dd4459b873cb15a8b03db43", "id": "323f1461-781d-4f01-8636-190335019a5c", "name": "pvm_sec"}}
安全组创建成功
安全组id323f1461-781d-4f01-8636-190335019a5c

{"security_group_rule": {"remote_group_id": null, "direction": "ingress", "protocol": "tcp", "description": "", "ethertype": "IPv4", "remote_ip_prefix": null, "port_range_max": 3306, "updated_at": "2021-12-14T04:36:24Z", "security_group_id": "323f1461-781d-4f01-8636-190335019a5c", "port_range_min": 20, "revision_number": 0, "tenant_id": "55b50cbb4dd4459b873cb15a8b03db43", "created_at": "2021-12-14T04:36:24Z", "project_id": "55b50cbb4dd4459b873cb15a8b03db43", "id": "e86c3d9a-3502-4720-8b28-247211e53c92"}}
安全组规则添加成功

 

posted @ 2021-12-14 15:10  衡衡酱  阅读(3)  评论(0)    收藏  举报
Live2D