小编Mal*_*amy的帖子

在 Terraform 0.12 中,如果资源名称已存在,如何跳过资源的创建?

我正在使用 Terraform 0.12 版。如果已存在具有相同名称的资源,我需要跳过资源创建。

我为此做了以下工作:

阅读自定义图像列表,

data "ibm_is_images" "custom_images" {
}
Run Code Online (Sandbox Code Playgroud)

检查图像是否已经存在,

locals {
 custom_vsi_image = contains([for x in data.ibm_is_images.custom_images.images: "true" if x.visibility == "private" && x.name == var.vnf_vpc_image_name], "true")
}

output "abc" {
value="${local.custom_vsi_image}"
}
Run Code Online (Sandbox Code Playgroud)

仅当图像存在时创建为假。

resource "ibm_is_image" "custom_image" {
  count            = "${local.custom_vsi_image == true ? 0 : 1}"
  depends_on       = ["data.ibm_is_images.custom_images"]
  href             = "${local.image_url}"
  name             = "${var.vnf_vpc_image_name}"
  operating_system = "centos-7-amd64"

  timeouts {
    create = "30m"
    delete = "10m"
  }
}
Run Code Online (Sandbox Code Playgroud)

这在第一次使用“terraform apply”时效果很好。它发现图像不存在,因此创建图像。

当我第二次运行“terraform apply”时。它正在删除上面创建的资源“custom_image”。知道为什么要在第二次运行时删除资源吗?

另外,如何根据某些条件创建资源(例如仅当它不存在时)?

resources exists terraform

25
推荐指数
1
解决办法
4万
查看次数

如何根据条件从 terraform 0.12 中的地图列表中获取值

我正在使用 Terraform 0.12。我有一个返回地图列表的数据源。下面是一个例子:

[
  {
    "name": "abc"
    "id": "123"
  },
  {
    "name": "bcd"
    "id": "345"
  }
] 
Run Code Online (Sandbox Code Playgroud)

如何遍历地图列表的这个数据源并查找键为“name”和值为“bcd”的地图是否存在?

这是我的数据源:

data "ibm_is_images" "custom_images" {}

locals {
  isexists = "return true/false based on above condition"
}
Run Code Online (Sandbox Code Playgroud)

如果存在,我想创建一个计数为 0 的资源,否则为 1

resource "ibm_is_image" "my_image" {
  count = local.isexists == "true" ? 0 : 1
}
Run Code Online (Sandbox Code Playgroud)

terraform

4
推荐指数
1
解决办法
1841
查看次数

Python Rest客户端api上传文件

我正在使用Python 2.7。我的 Rest 服务器端 API 工作正常,我可以使用 Postman 上传 zip 文件。我正在尝试使用 Rest 客户端 api 上传 zip 文件。我尝试了请求包,但无法发送文件。我收到错误:缺少文件参数。

这是我的 python 服务器端代码:

@ns.route('/upload_file', strict_slashes=False)
class Upload(Resource):
    @api.expect(upload_parser)
    def post(self):

        args = upload_parser.parse_args()
        file_nameup = args.file.filename 
Run Code Online (Sandbox Code Playgroud)

这是其余的 api 客户端代码:

import requests
from requests.auth import HTTPBasicAuth
import json

headers={'Username': 'abc@gmail.com', 'apikey':'123-e01b', 'Content-Type':'application/zip'}
f = open('C:/Users/ADMIN/Downloads/abc.zip', 'rb')

files = {"file": f}

resp = requests.post("https://.../analytics/upload_file", files=files, headers=headers )

print resp.text   

print "status code " + str(resp.status_code)

if resp.status_code == 200:
    print ("Success")
    print resp.json()
else:
    print ("Failure") …
Run Code Online (Sandbox Code Playgroud)

python api rest upload

2
推荐指数
1
解决办法
4万
查看次数

Java 8:<identifier> 预期

这是我的代码:

CloseableHttpResponse closeableResponse = callApi(request);
int status = closeableResponse.getStatusLine().getStatusCode();
logger.info("Response Code : {}", status);

StringBuilder result = new StringBuilder();
BufferedReader rd = null;
try (rd = new BufferedReader(new InputStreamReader(closeableResponse.getEntity().getContent()))) {
    String line = "";
    while ((line = rd.readLine()) != null) {
        result.append(line);
    }
} finally {
    if (rd != null) rd.close();
}
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

在此以下行中预期的标识符:

rd = 新的 BufferedReader(InputStreamReader(closeableResponse.getEntity().getContent())))

对此有什么想法吗?我已经定义了所有变量。为什么我收到这个错误?

java identifier

2
推荐指数
1
解决办法
238
查看次数

标签 统计

terraform ×2

api ×1

exists ×1

identifier ×1

java ×1

python ×1

resources ×1

rest ×1

upload ×1