码迷,mamicode.com
首页 > 编程语言 > 详细

python 获取SLB信息 更换证书

时间:2018-01-24 12:39:26      阅读:361      评论:0      收藏:0      [点我收藏+]

标签:with   带宽   div   request   session   path   会话保持   time   python3   

首先安装阿里云SDK

pip install aliyun-python-sdk-core

pip install aliyun-python-sdk-slb

可以配合jenkins传递参数

#获取slb的信息

#!/usr/bin/python3
# -*- coding: utf-8 -*-
# @Time    : 2018/1/18 0018 16:51
# @Author  : sanmuyan
# @File    : aliyun_slb.py
import json, jsonpath, os
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkslb.request.v20140515 import DescribeLoadBalancersRequest


def slb_info(AccessKey,Secret,Regions,slb_name,AddressType):
    # 创建 AcsClient 实例
    client = AcsClient(
        AccessKey,
        Secret,
        Regions
        );
    # 查看负载均衡实例
    request = DescribeLoadBalancersRequest.DescribeLoadBalancersRequest()
    if (slb_name != "all"):
        request.set_LoadBalancerName(slb_name)
    else:
        request.set_LoadBalancerName("")

    if (AddressType != "all"):
        request.set_AddressType(AddressType)
    else:
        request.set_AddressType("")
    response = client.do_action_with_exception(request)
    data = json.loads(response)
    data1 = jsonpath.jsonpath(data, "$.LoadBalancers.LoadBalancer.[*].LoadBalancerId")
    data2 = jsonpath.jsonpath(data, "$.LoadBalancers.LoadBalancer.[*].Address")
    data3 = jsonpath.jsonpath(data, "$.LoadBalancers.LoadBalancer.[*].LoadBalancerName")

    print("SLB基本信息")
    for i in range(len(data1)):
        print(data1[i], =, data2[i], =, data3[i])
    data = json.dumps(data, sort_keys=True, indent=2)
    data = data.encode(utf-8).decode(unicode_escape)

    print("打印出JSON格式数据")
    print(data)

if __name__ == __main__:    
    AccessKey = os.environ.get(AccessKey)
    Secret = os.environ.get(Secret)
    Regions = os.environ.get(Regions)
    slb_name = os.environ.get(slb_name)
    AddressType = os.environ.get(AddressType)
    slb_info (AccessKey,Secret,Regions,slb_name,AddressType)

 

#更换证书

#!/usr/bin/python3
# -*- coding: utf-8 -*-
# @Time    : 2018/1/18 0018 16:51
# @Author  : sanmuyan
# @File    : aliyun_slb.py
import json, jsonpath, os

from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkslb.request.v20140515 import DescribeLoadBalancerHTTPSListenerAttributeRequest
from aliyunsdkslb.request.v20140515 import SetLoadBalancerHTTPSListenerAttributeRequest

def slb_set (LoadBalancerId,ListenerPort,ServerCertificateId,AccessKey,Secret,Regions):
    # 创建 AcsClient 实例
    client = AcsClient(
        AccessKey,
        Secret,
        Regions
    );

    # 获取配置前的信息
    request = DescribeLoadBalancerHTTPSListenerAttributeRequest.DescribeLoadBalancerHTTPSListenerAttributeRequest()
    request.set_LoadBalancerId(LoadBalancerId)
    request.set_ListenerPort(ListenerPort)
    response = client.do_action_with_exception(request)
    data = (json.loads(response))
    HealthCheck = (data[HealthCheck])

    print("更新前的配置信息")
    print(json.dumps(data, sort_keys=True, indent=2))

    # 设置负载均衡实例
    request = SetLoadBalancerHTTPSListenerAttributeRequest.SetLoadBalancerHTTPSListenerAttributeRequest()
    request.set_LoadBalancerId(LoadBalancerId)  # 负载均衡实例的唯一标识
    request.set_ListenerPort(ListenerPort)  # 负载均衡实例前端使用的端口
    request.set_ServerCertificateId(ServerCertificateId)  # 证书ID
    request.set_Bandwidth(data[Bandwidth])  # 监听的带宽峰值。取值:-1/1-1000Mbps
    request.set_StickySession(data[StickySession])  # 是否开启会话保持
    request.set_HealthCheck(HealthCheck)  # 是否开启健康检查

    if (HealthCheck == "on"):
        request.set_HealthCheckURI(data[HealthCheckURI])  # 监控检查url
        request.set_HealthCheckTimeout(data[HealthCheckTimeout])  # 健康检查超时时间
        request.set_HealthCheckInterval(data[HealthCheckInterval])  # 健康检查间隔时间
        request.set_HealthyThreshold(data[HealthyThreshold])  # 健康检查健康阈值
        request.set_UnhealthyThreshold(data[UnhealthyThreshold])  # 健康检查不健康阈值
        if "HealthCheckConnectPort" in data:
            request.set_HealthCheckConnectPort(data[HealthCheckConnectPort])  # 健康检查端口
        else:
            print("HealthCheckConnectPort=default")
    else:
        print("HealthCheck=off")

    response = client.do_action_with_exception(request)

    print(response)

    print("更新后的配置信息")
    print(json.dumps(data, sort_keys=True, indent=2))


if __name__ == __main__:
    LoadBalancerId = os.environ.get(LoadBalancerId)
    ListenerPort = os.environ.get(ListenerPort)
    ServerCertificateId = os.environ.get(ServerCertificateId)
    AccessKey = os.environ.get(AccessKey)
    Secret = os.environ.get(Secret)
    Regions = os.environ.get(Regions)
    slb_set (LoadBalancerId,ListenerPort,ServerCertificateId,AccessKey,Secret,Regions)

 

python 获取SLB信息 更换证书

标签:with   带宽   div   request   session   path   会话保持   time   python3   

原文地址:https://www.cnblogs.com/37yan/p/8340880.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!