github.com/lukasheimann/cloudfoundrycli@v7.1.0+incompatible/resources/security_group_resource.go (about)

     1  package resources
     2  
     3  import (
     4  	"encoding/json"
     5  
     6  	"code.cloudfoundry.org/jsonry"
     7  )
     8  
     9  type SecurityGroup struct {
    10  	Name                   string   `jsonry:"name,omitempty"`
    11  	GUID                   string   `jsonry:"guid,omitempty"`
    12  	Rules                  []Rule   `jsonry:"rules,omitempty"`
    13  	StagingGloballyEnabled *bool    `jsonry:"globally_enabled.staging,omitempty"`
    14  	RunningGloballyEnabled *bool    `jsonry:"globally_enabled.running,omitempty"`
    15  	StagingSpaceGUIDs      []string `jsonry:"relationships.staging_spaces.data[].guid,omitempty"`
    16  	RunningSpaceGUIDs      []string `jsonry:"relationships.running_spaces.data[].guid,omitempty"`
    17  }
    18  
    19  func (sg SecurityGroup) MarshalJSON() ([]byte, error) {
    20  	return jsonry.Marshal(sg)
    21  }
    22  
    23  func (sg *SecurityGroup) UnmarshalJSON(data []byte) error {
    24  	type alias SecurityGroup
    25  	var defaultUnmarshalledSecurityGroup alias
    26  	err := json.Unmarshal(data, &defaultUnmarshalledSecurityGroup)
    27  	if err != nil {
    28  		return err
    29  	}
    30  
    31  	*sg = SecurityGroup(defaultUnmarshalledSecurityGroup)
    32  
    33  	type RemainingFieldsStruct struct {
    34  		GloballyEnabled struct {
    35  			Staging *bool
    36  			Running *bool
    37  		} `json:"globally_enabled"`
    38  		Relationships struct {
    39  			StagingSpaces struct {
    40  				Data []struct {
    41  					Guid string
    42  				}
    43  			} `json:"staging_spaces"`
    44  			RunningSpaces struct {
    45  				Data []struct {
    46  					Guid string
    47  				}
    48  			} `json:"running_spaces"`
    49  		}
    50  	}
    51  
    52  	var remainingFields RemainingFieldsStruct
    53  	err = json.Unmarshal(data, &remainingFields)
    54  	if err != nil {
    55  		return err
    56  	}
    57  
    58  	for _, stagingSpace := range remainingFields.Relationships.StagingSpaces.Data {
    59  		sg.StagingSpaceGUIDs = append(sg.StagingSpaceGUIDs, stagingSpace.Guid)
    60  	}
    61  
    62  	for _, runningSpace := range remainingFields.Relationships.RunningSpaces.Data {
    63  		sg.RunningSpaceGUIDs = append(sg.RunningSpaceGUIDs, runningSpace.Guid)
    64  	}
    65  
    66  	sg.StagingGloballyEnabled = remainingFields.GloballyEnabled.Staging
    67  	sg.RunningGloballyEnabled = remainingFields.GloballyEnabled.Running
    68  
    69  	return nil
    70  }
    71  
    72  type Rule struct {
    73  	Protocol    string  `json:"protocol"`
    74  	Destination string  `json:"destination"`
    75  	Ports       *string `json:"ports,omitempty"`
    76  	Type        *int    `json:"type,omitempty"`
    77  	Code        *int    `json:"code,omitempty"`
    78  	Description *string `json:"description,omitempty"`
    79  	Log         *bool   `json:"log,omitempty"`
    80  }