简体   繁体   English

AWS Cloudformation JSON模板到C#对象

[英]AWS Cloudformation JSON template to c# object

Does anyone know how to convert an AWS cloud formation son template into ac# object or custom class. 有谁知道如何将AWS云形成子模板转换为ac#对象或自定义类。 I've deserialised json using a data contract before but I'm having trouble with the cloud formation template because each resource starts with a unique name so I'm not sure how to handle it. 我之前使用数据协定对json进行反序列化,但是我在使用云形成模板时遇到了麻烦,因为每个资源都以唯一的名称开头,所以我不确定如何处理它。 My aim is to compare a template with what is already live in AWS by putting the data from the API and the data from the template into a class and comparing them. 我的目标是通过将API中的数据和模板中的数据放入一个类中并进行比较,从而将模板与AWS中已有的内容进行比较。 If there's a better way please feel free to shoot me down. 如果有更好的方法,请随时将我击落。 Here's and example cloud formation template. 这是示例云形成模板。

{
  "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" : {    
    }
  }
}

The straightforward way would be to treat the Resources part of the template as a dictionary where the resource name is the key and its properties is the value: 直接的方法是将模板的“ 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; }
}

(The rest of the fields is obvious) (其余字段很明显)

And then using 然后使用

var template = JsonConvert.DeserializeObject<Template>(json);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM