簡體   English   中英

AWS Cloudformation JSON模板到C#對象

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM