阅读 91

powershell脚本创建3scale api

使用powershell来创建3scale api,一个是ps文件,一个是csv文件

ps文件如下,api name是hard code在里面xtest1,忽略https自签证书验证

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

##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

##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

 

原文:https://www.cnblogs.com/caihemm/p/13690078.html

文章分类
代码人生
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐