华为云的API调用实践(python版本)
一、结论:
1、华为云是符合openstack 社区的API,所以,以社区的API为准。社区API见下面的链接。
https://developer.openstack.org/api-ref/network/v2/index.html
二、调用前准备
1、python的API调用前,需要准备好python的运行环境,以及在华为云上已经注册用户。
python的运行环境具体是指(1)python安装包(2)openstack社区的API库,下面有介绍。
华为云注册用户,正常在官网上注册即可。
2、python的安装包
略(到python官网下载 python2.7版本,安装即可)
3、openstack社区的API库(python版本)
(1)下载openstack 客户端开发包,地址如下,下载其中的.gz压缩包
https://pypi.python.org/pypi/python-openstackclient
(2)安装客户端开发包
在windows系统中,解压缩上述压缩包,然后进入到解压缩后的文件夹中,输入 python setup.py install
4、获取username,project_name, project_domain_id, user_domain_id
方法:
第一步,在华为云登录后,在页面右上角点击 “我的凭证”

第二步、在我的凭证中,分别获得 username, project_name, project_domain_id, user_domain_id


5、获得所在区域的IAM认证的地址
方法:
第一步:华为云首页,“支持与服务” ,选择"OpenAPI"

第二步:点击“地区和终端节点”

第三步:在搜索栏中输入“IAM”,得到所在区域的IAM认证地址。

上面得到的终端节点,就是 auth_url 的内容主体。
三、API调用思路:
1、调用思路:
认证 ---> 建会话 ---> 建客户端实例 ---> API调用。
2、程序示例:
说明,下面代码中的 username, project_name, project_domain_id, user_domain_id, auth_url 的内容获取方法,见“二、调用前准备”中的对应内容。

四、调用步骤
根据API文档,以及 client.Client的代码,可以看到有哪些API可以调用。
1、示例代码
import json
import time
from keystoneauth1 import identity
from keystoneauth1 import session
from neutronclient.v2_0 import client username='xxx'
password='xxx'
project_name='xxx'
project_domain_id='xxx'
user_domain_id='xxx'
auth_url='https://iam.cn-north-1.myhuaweicloud.com/v3'
auth = identity.Password(auth_url=auth_url,
username=username,
password=password,
project_name=project_name,
project_domain_id=project_domain_id,
user_domain_id=user_domain_id)
sess = session.Session(auth=auth)
neutron = client.Client(session=sess) def createvpn(vpcid, local_cidr, peer_ip, peer_cidr):
print "######## create vpn ######"
print "################### step 1 vpn service ############" vpnservice = {
"vpnservice": {
"router_id": vpcid,
"name": "myservice",
"admin_state_up": "true"
}
} ret = neutron.create_vpnservice(vpnservice) vpnserviceid = ret['vpnservice']['id']
print "vpnserviceid = "+vpnserviceid print "public_ip = "+ret['vpnservice']['external_v4_ip'] print "################### step 2 ike policy ############" ikepolicy = {
"ikepolicy": {
"phase1_negotiation_mode": "main",
"auth_algorithm": "sha1",
"encryption_algorithm": "aes-128",
"pfs": "group5",
"lifetime": {
"units": "seconds",
"value": 86400
},
"ike_version": "v1",
"name": "ikepolicy1"
}
} ret = neutron.create_ikepolicy(ikepolicy) ikepolicyid = ret['ikepolicy']['id']
print "ikepolicyid = "+ikepolicyid print "################### step 3 ipsec policy ############" ipsecpolicy = {
"ipsecpolicy": {
"name": "ipsecpolicy1",
"transform_protocol": "esp",
"auth_algorithm": "sha1",
"encapsulation_mode": "tunnel",
"encryption_algorithm": "aes-128",
"pfs": "group5",
"lifetime": {
"units": "seconds",
"value": 3600
}
}
} ret = neutron.create_ipsecpolicy(ipsecpolicy) ipsecpolicyid = ret['ipsecpolicy']['id']
print "ipsecpolicyid = "+ipsecpolicyid print "################### step 4 local and remote endpoints ############" localendpointgroup = {
"endpoint_group": {
"endpoints": local_cidr,
"type": "cidr",
"name": "my-localendpoints"
}
} ret = neutron.create_endpoint_group(localendpointgroup) localepgroupid = ret['endpoint_group']['id']
print "localepgroupid = "+localepgroupid #### remote endpoint group
remoteendpointgroup = {
"endpoint_group": {
"endpoints": peer_cidr,
"type": "cidr",
"name": "remote-localendpoints"
}
} ret = neutron.create_endpoint_group(remoteendpointgroup) remoteepgroupid = ret['endpoint_group']['id']
print "remoteepgroupid = "+remoteepgroupid print "################### step 5 ipsec connection ############" ipsecconnection = {
"ipsec_site_connection": {
"psk": "secret",
"initiator": "bi-directional",
"ipsecpolicy_id": ipsecpolicyid,
"admin_state_up": "true",
"mtu": "",
"peer_ep_group_id": remoteepgroupid,
"ikepolicy_id": ikepolicyid,
"vpnservice_id": vpnserviceid,
"local_ep_group_id": localepgroupid,
"peer_address": peer_ip,
"peer_id": peer_ip,
"name": "vpnconnection1"
}
} ret = neutron.create_ipsec_site_connection(ipsecconnection) ipsec_connection_id = ret['ipsec_site_connection']['id']
print "ipsec_connection_id = "+ipsec_connection_id def update_peer_ip(masterip, backupip):
print "######## update peer ip ######"
newcontent = {
"ipsec_site_connection": {
"peer_address": backupip,
"peer_id":backupip
}
} connections = neutron.list_ipsec_site_connections() entrys = connections['ipsec_site_connections']
for entry in entrys:
if (masterip == entry["peer_address"]):
id = entry["id"]
neutron.update_ipsec_site_connection(id, newcontent)
time.sleep(3) def show_all_connections():
print "######## show all ipsec connections "
connections = neutron.list_ipsec_site_connections()
entrys = connections['ipsec_site_connections']
for entry in entrys:
print entry def main(): createvpn(vpcid="xxx",
local_cidr=["10.2.0.0/24","10.3.0.0/24"],
peer_ip="172.24.4.233",
peer_cidr=["20.2.0.0/24"]) show_all_connections()
#update_peer_ip(masterip="172.24.4.233", backupip="101.0.0.1") if __name__ == '__main__':
main()
2、查看有哪些函数可以调用。

华为云的API调用实践(python版本)的更多相关文章
- 阿里云 API调用实践(python语言)
1.结论:阿里云的SDK开发,其实就是远程调用API,python的代码就是一个外壳,核心是封装成一个http报文,利用json格式,进行RPC调用. 2.SDK调用API的套路如下: # -*- c ...
- 如何用Baas快速在腾讯云上开发小程序-系列3 :实现腾讯云COS API调用
版权声明:本文由贺嘉 原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/640268001487425627 来源:腾云阁 h ...
- 【转+修改】容联云通讯api调用短信发送调用
转自 https://my.oschina.net/u/1995134/blog/814540 需要荣联云通讯 的 相对应SDKjar包. CCP_REST_SMS_SDK_JAVA_v2.6.3 ...
- 用Python调用华为云API接口发短信
[摘要] 用Python调用华为云API接口实现发短信,当然能给调用发短信接口前提条件是通过企业实名认证,而且有一个通过审核的短信签名,话不多说,showcode #!/usr/bin/python3 ...
- 微信公众平台开发(免费云BAE+高效优雅的Python+网站开放的API)
虽然校园App是个我认为的绝对的好主意,但最近有个也不错的营销+开发的模式出现:微信平台+固定域名服务器. 微信公众平台的运行模式不外两个: 一.机器人模式或称转发模式,将说话内容转发到服务器上完成, ...
- MarkDown添加图片的三种方式【华为云技术分享】
Markdown插图片有三种方法,各种Markdown编辑器的插图方式也都包含在这三种方法之内. 插图最基础的格式就是: 在DevOps CMALS理念中具有支柱性地位,因而CI/CD流水线 ...
随机推荐
- nginx+letsencrypt搭建https站点
1. 申请好自己的域名 dockerhub.xxx.com,并解析好IP. 2. 安装nginx(默认开通了http) ,修改 server_name dockerhub.xxxx.com; 启动. ...
- Tomcat 集群中 实现session 共享的三种方法
前两种均需要使用 memcached 或 redis 存储 session ,最后一种使用 terracotta 服务器共享. 建议使用 redis ,不仅仅因为它可以将缓存的内容持久化,还因为它支持 ...
- MyEclipse设置Console输出到文件
Java程序默认输出为Console,如果要想将Console输出结果保存到文件中,则需要做如下配置: 在JAVA程序上右键--> Run As --> Run Configuration ...
- [Swift]LeetCode72. 编辑距离 | Edit Distance
Given two words word1 and word2, find the minimum number of operations required to convert word1 to ...
- [Swift]LeetCode99. 恢复二叉搜索树 | Recover Binary Search Tree
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- django中的跨表查询梳理
1.前言 最近在写一个小项目,里面主要涉及的就是表与表之间复杂的关系.当真正开发起来的时候,才发现自己对复杂的表关系间的查询有点混乱,趁着这几天的时间,重新梳理了一下. 2.概念 在开始之前,先明确几 ...
- Kubernetes因限制内存配置引发的错误
今天对一个pod进行内存资源调整后, 一直卡在ContainerCreating的状态, 执行describe命令查看该 Pod 详细信息后发现如下 . [root@master-01 ~]# kub ...
- Spring Boot 2.0 教程 | 配置 Undertow 容器
欢迎关注个人微信公众号: 小哈学Java, 文末分享阿里 P8 资深架构师吐血总结的 <Java 核心知识整理&面试.pdf>资源链接!! 文章首发于个人网站 https://ww ...
- JVM基础系列第1讲:Java 语言的前世今生
Java 语言是一门存在了 20 多年的语言,其年纪比我自己还大.虽然存在了这么长时间,但 Java 至今都是最大的工业级语言,许多大型互联网公司均采用 Java 来实现其业务系统.大到国际电商巨头阿 ...
- Unity GC 优化要点
参考:http://blog.csdn.net/znybn1/article/details/76464896 为啥要点?因为讲的重点. 游戏运行时来存储数据,当这些数据不再被使用时,存储这些数据的内 ...