zoukankan      html  css  js  c++  java
  • powershell脚本创建3scale api

    使用powershell来创建3scale api,

    3scale提供了管理api,rest服务,通过这些api可以操作3scale,代替界面鼠标点点

    powershell里使用Invoke-WebRequest这个函数来调用rest服务,

    有个注意点是要忽略https自签证书验证

    还有个注意点是设置出错的时候停止运行,因为默认有错误还是会继续往下执行

    总共2个文件,一个是ps文件,一个是csv文件

    ps文件如下,api name是hard code在里面xtest1,

    csv文件里有3个字段,一个是http 方法 get post等,另外一个计量名字,api resource的缩写,统计用,还有一个是resource

    好处是使用windows原生工具,不用安装其他任何工具,比如curl,go,java,python等。

    局限就是仅限windows,当然现在也可以在linux装powershell,但是估计没多少人会那么干。

    扯了那么多废话,上代码

    ##env var
    $base_url="https://xxxxxxx"
    $access_token="xxxxxxxxx"
    $rules_file="rules.csv"
    ##service
    $service_name="xtest1"
    ##paths
    $path_method_create="/admin/api/services/{service_id}/metrics/{metric_id}/methods.xml"
    $path_mapping_rule_create="/admin/api/services/{service_id}/proxy/mapping_rules.xml"
    $path_metric_list="/admin/api/services/{service_id}/metrics.xml"
    $path_service_create="/admin/api/services.xml"
    ##paras
    $content_type="application/x-www-form-urlencoded"
    $access_token_para="?access_token="+$access_token
    
    ##ignore self sign certificate
    add-type @"
        using System.Net;
        using System.Security.Cryptography.X509Certificates;
        public class TrustAllCertsPolicy : ICertificatePolicy {
            public bool CheckValidationResult(
                ServicePoint srvPoint, X509Certificate certificate,
                WebRequest request, int certificateProblem) {
                return true;
            }
        }
    "@
    [System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
      $ErrorActionPreference="Stop"
    ##service create
    "create service"
    $full_url=$base_url + $path_service_create
    $body=@{
        access_token=$access_token
        name=$service_name
    }
    $reponse=Invoke-WebRequest -Method POST -Uri $full_url -body $body -ContentType $content_type
    $service_id=([xml]$reponse.Content).service.id
    $service_id
    
    ##get metric hits id
    "get hits id"
    $full_url=$base_url + $path_metric_list + $access_token_para -replace "{service_id}",$service_id
    $reponse=Invoke-WebRequest -Method GET -Uri $full_url
    $metric_id=([xml]$reponse.Content).metrics.metric.id
    $metric_id
    
    ##create method and mapping rules from config file
    $rules=Import-Csv $rules_file
    foreach($rule in $rules){
        $verb=$rule.verb
        $method=$rule.method
        $pattern=$rule.pattern
                
        ##create method
        "create method " + $method
        $full_url=$base_url + $path_method_create -replace "{service_id}",$service_id -replace "{metric_id}",$metric_id
        $body=@{
            access_token=$access_token
            friendly_name=$method
            unit="hit"
        }
        $reponse=Invoke-WebRequest -Method POST -Uri $full_url -body $body -ContentType $content_type
        $method_id=([xml]$reponse.Content).method.id
        $method_id
    
        ##create mapping rule
        "create mapping rule " + $verb + " " + $pattern
        $pattern
        $full_url=$base_url + $path_mapping_rule_create -replace "{service_id}",$service_id
        $body=@{
            access_token=$access_token
            http_method=$verb
            pattern=$pattern
            delta="1"
            metric_id=$method_id
        }
        $reponse=Invoke-WebRequest -Method POST -Uri $full_url -body $body -ContentType $content_type
        $rule_id=([xml]$reponse.Content).mapping_rule.id
    }

    csv文件

    verb,method,pattern
    GET,xxxx,/x
    POST,fdsaf,/y
    PUT,zzzzz,/z
  • 相关阅读:
    编译nginx
    MVPN技术原理
    python中_, __, __foo__区别及使用场景
    https双向认证(python)
    http keepalive test code(python)
    压缩 KVM 的 qcow2 镜像文件
    nohup python程序,print无输出
    Less(51)
    Less(50)
    Less(49)
  • 原文地址:https://www.cnblogs.com/caihemm/p/13690078.html
Copyright © 2011-2022 走看看