一、结论:

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版本)的更多相关文章

  1. 阿里云 API调用实践(python语言)

    1.结论:阿里云的SDK开发,其实就是远程调用API,python的代码就是一个外壳,核心是封装成一个http报文,利用json格式,进行RPC调用. 2.SDK调用API的套路如下: # -*- c ...

  2. 如何用Baas快速在腾讯云上开发小程序-系列3 :实现腾讯云COS API调用

    版权声明:本文由贺嘉 原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/640268001487425627 来源:腾云阁 h ...

  3. 【转+修改】容联云通讯api调用短信发送调用

    转自   https://my.oschina.net/u/1995134/blog/814540 需要荣联云通讯 的 相对应SDKjar包. CCP_REST_SMS_SDK_JAVA_v2.6.3 ...

  4. 用Python调用华为云API接口发短信

    [摘要] 用Python调用华为云API接口实现发短信,当然能给调用发短信接口前提条件是通过企业实名认证,而且有一个通过审核的短信签名,话不多说,showcode #!/usr/bin/python3 ...

  5. 微信公众平台开发(免费云BAE+高效优雅的Python+网站开放的API)

    虽然校园App是个我认为的绝对的好主意,但最近有个也不错的营销+开发的模式出现:微信平台+固定域名服务器. 微信公众平台的运行模式不外两个: 一.机器人模式或称转发模式,将说话内容转发到服务器上完成, ...

  6. MarkDown添加图片的三种方式【华为云技术分享】

    Markdown插图片有三种方法,各种Markdown编辑器的插图方式也都包含在这三种方法之内. 插图最基础的格式就是: ![Alt text](图片链接 "optional title&q ...

  7. 大海航行靠舵手 华为云靠什么征服K8S?

    Kubernetes 是Google开源的容器集群管理系统或者称为分布式操作系统.它构建在Docker技术之上,为容器化的应用提供资源调度.部署运行.服务发现.扩容缩容等整一套功能,本质上可看作是基于 ...

  8. 华为云发布桌面IDE-CodeArts

    摘要:华为伙伴暨开发者大会2022,发布华为云桌面IDE-CodeArts. 本文分享自华为云社区<华为云发布桌面IDE-CodeArts,让连接更简单.编码更智能>,作者: Huawei ...

  9. DevOps on DevCloud|如何采用流水线践行CI/CD理念【华为云技术分享】

    [摘要] 持续集成/持续交付(CI/CD,Continuous Integration/Continuous Deployment)在DevOps CMALS理念中具有支柱性地位,因而CI/CD流水线 ...

随机推荐

  1. Android OpenSL ES 开发:使用 OpenSL 播放 PCM 数据

    OpenSL ES 是基于NDK也就是c语言的底层开发音频的公开API,通过使用它能够做到标准化, 高性能,低响应时间的音频功能实现方法. 这次是使用OpenSL ES来做一个音乐播放器,它能够播放m ...

  2. [Swift]LeetCode20. 有效的括号 | Valid Parentheses

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  3. [Swift]LeetCode640. 求解方程 | Solve the Equation

    Solve a given equation and return the value of x in the form of string "x=#value". The equ ...

  4. Metrics.NET step by step

    安装Nuget包 nuget中搜索metrics,如图: 配置Metrics 在程序入口处插入配置Metrics的代码. class Program { static void Main(string ...

  5. 论JVM爆炸的几种姿势及自救方法,你不得不知!

    前言 如今不管是在面试还是在我们的工作中,OOM总是不断的出现在我们的视野中,所以我们有必要去了解一下导致OOM的原因以及一些基本的调整方法,大家可以通过下面的事例来了解一下什么样的代码会导致OOM, ...

  6. 【机器学习】--线性回归中soft-max从初始到应用

    一.前述 Soft-Max是做多分类的,本身是哪个类别的概率大,结果就为对应的类别.为什么称之为Soft判别,原因是归一化之后的概率选择最大的作为结果,而不是只根据分子. 二.原理 sigmod函数: ...

  7. qt deleterLater

    原文链接:浅谈 Qt 内存管理     Qt 内存管理是本文将要介绍的内容,在QT的程序中经常会看到只有new而不delete的情况,其实是因为QT有一套回收内存的机制,主要的规则如下: 1.所有继承 ...

  8. 计蒜客:Entertainment Box

    Ada, Bertrand and Charles often argue over which TV shows to watch, and to avoid some of their fight ...

  9. Chapter 5 Blood Type——11

    "I just wondered… if you could warn me beforehand the next time you decide to ignore me for my ...

  10. Dockerfile 中的 COPY 与 ADD 命令

    Dockerfile 中提供了两个非常相似的命令 COPY 和 ADD,本文尝试解释这两个命令的基本功能,以及其异同点,然后总结其各自适合的应用场景. Build 上下文的概念 在使用 docker ...