[英]AWS Cloudformation JSON template to c# object
有谁知道如何将AWS云形成子模板转换为ac#对象或自定义类。 我之前使用数据协定对json进行反序列化,但是我在使用云形成模板时遇到了麻烦,因为每个资源都以唯一的名称开头,所以我不确定如何处理它。 我的目标是通过将API中的数据和模板中的数据放入一个类中并进行比较,从而将模板与AWS中已有的内容进行比较。 如果有更好的方法,请随时将我击落。 这是示例云形成模板。
{
"AWSTemplateFormatVersion" : "2010-09-09",
"Description" : "AWS CloudFormation Sample.",
"Parameters" : {
"KeyName": {
"Description" : "Name of an existing EC2 KeyPair to enable SSH access to the instance",
"Type": "AWS::EC2::KeyPair::KeyName",
"ConstraintDescription" : "must be the name of an existing EC2 KeyPair."
},
"Resources" : {
"SecurityGroup1" : {
"Type" : "AWS::EC2::SecurityGroup",
"Properties" : {
"GroupDescription" : "Enable SSH access via port 22",
"SecurityGroupIngress" : [ {
"IpProtocol" : "tcp",
"FromPort" : "22",
"ToPort" : "22",
"CidrIp" : { "Ref" : "SSHLocation"}
} ]
}
},
"SecurityGroup2" : {
"Type" : "AWS::EC2::SecurityGroup",
"Properties" : {
"GroupDescription" : "Enable SSH access via port 22",
"SecurityGroupIngress" : [ {
"IpProtocol" : "tcp",
"FromPort" : "22",
"ToPort" : "22",
"CidrIp" : { "Ref" : "SSHLocation"}
} ]
}
}
},
"Outputs" : {
}
}
}
直接的方法是将模板的“ Resources
部分视为字典,其中资源名称是键,其属性是值:
class ResourceProperties
{
public string GroupDescription { get; set; }
}
class Resource
{
public string Type { get; set; }
public ResourceProperties Properties { get; set; }
}
class Parameters
{
public Dictionary<string, Resource> Resources { get; set; }
}
class Template
{
public Parameters Parameters { get; set; }
}
(其余字段很明显)
然后使用
var template = JsonConvert.DeserializeObject<Template>(json);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.