2013-05-20 29 views
0

我需要显示在ec2上运行的服务器信息。我设法将它们显示为json,但现在我只需要显示某些字段。以下显示我如何显示statusCodeRunType。类似的概念适用于其他领域。如何在grails的这种情况下使用map?

def check = reservations.each { Reservation reservation -> 
     reservation.instances.each() { Instance instance -> 
      def code = instance.state 
      def StatusCode = code.code 
//Get other values 


      instance.tags.each() { Tag tag5 -> 
       def KeyName2 = tag5.key; 
       if (KeyName2 == 'RunType') { 
        def RunType = tag5.value; 
       } 
      } 
     } 

instance.tags.each() { Tag tag2 -> 

       def KeyName2 = tag2.key; 

       if (KeyName2 == 'RunType') { 
        def value2 = tag2.value;     } 
      } 

      instance.tags.each() { Tag tag3 -> 

       def KeyName3 = tag3.key; 

       if (KeyName3 == 'StartedBy') { 
        def value = tag3.value; 
       } 
      } 

我想获得这样的事情对每个服务器在我的GSP页面,并循环播放显示。

def map = [StatusCode:"80", RunType:"Always", value"test", value2:"abc"] 

,但不知道如何添加值当我得到他们直通代码

+1

您可以编辑您的问题补充那种你所期望得到的地图是形状后更新? –

+0

那么,你想要一个地图列表?这很难看到你有什么,你想要什么... –

+0

是的,我想你必须最终使用地图列表。请参阅下面的答案。 – dmahapatro

回答

1

您可能能够使用此:

def result = reservations.collectMany { reservation -> 
    reservation.instances.collect { instance -> 
    [ StatusCode: instance.code, 
     RunType : instance.tags.find { it.key == 'RunType' }?.value, 
     StartedBy : instance.tags.find { it.key == 'StartedBy' }?.value ] 
    } 
} 

更新的问题

0

从它看起来像代码中的地图,的StatusCode可以有多个value5。如果不是这种情况,那么你可以这样做:

def myList = [] 
def check = reservations.each { Reservation reservation -> 
       reservation.instances.each() { Instance instance -> 
       def statusCode = instance.state?.code 
       def value5 = instance.tags?.find{it.key == 'RunType'}?.value 
       myList << ["StatusCode": statusCode, "RunType": value5 ] 
      } 
     } 

请注意,内循环是简化的。我不确定它是否满足您的用例场景,因为有内部循环。如果您认为地图需要在循环中向下移动一层,那么您还要在列表中维护键值对(或地图)的列表。

使用groovy的collectinject功能可以过度简化逻辑。

+0

StatusCode和Value5 r不同的字段,我想我很困惑你。 Value5是runtype。 –

+0

您可以在问题中显示一个结构化的虚拟地图,以了解您想如何获得最终结果? – dmahapatro

+0

类似于我在那里的地图 –