func updateSecurityGroupRule(cmd *Command, args []string) error { var rule rulesDefinition var content api.ScalewayNewSecurityGroupRule if len(args) != 3 { return cmd.PrintShortUsage() } if err := json.Unmarshal([]byte(args[2]), &rule); err != nil { return err } content.Action = rule.Action content.Direction = rule.Direction content.IPRange = rule.IPRange content.Protocol = rule.Protocol if rule.DestPortFrom != nil { content.DestPortFrom = *rule.DestPortFrom } return cmd.API.PutSecurityGroupRule(content, args[0], args[1]) }
func runSecurityGroups(cmd *Command, args []string) error { if securityGroupsHelp { return cmd.PrintUsage() } if securityGroupsNew != "" { var newGroups api.ScalewayNewSecurityGroup newParts := strings.SplitN(securityGroupsNew, ":", 2) if len(newParts) != 2 { return cmd.PrintShortUsage() } newGroups.Organization = cmd.API.Organization newGroups.Name = newParts[0] newGroups.Description = newParts[1] resp, err := cmd.API.PostResponse("security_groups", newGroups) if err != nil { return err } defer resp.Body.Close() // Succeed POST code if resp.StatusCode == 201 { return nil } var error api.ScalewayAPIError decoder := json.NewDecoder(resp.Body) err = decoder.Decode(&error) if err != nil { return err } error.StatusCode = resp.StatusCode error.Debug() return error } else if securityGroupsUpdate != "" { var newGroups api.ScalewayNewSecurityGroup if len(args) != 1 { return cmd.PrintShortUsage() } newParts := strings.SplitN(securityGroupsUpdate, ":", 2) if len(newParts) != 2 { return cmd.PrintShortUsage() } newGroups.Organization = cmd.API.Organization newGroups.Name = newParts[0] newGroups.Description = newParts[1] resp, err := cmd.API.PutResponse(fmt.Sprintf("security_groups/%s", args[0]), newGroups) if err != nil { return err } defer resp.Body.Close() // Succeed PUT code if resp.StatusCode == 200 { return nil } var error api.ScalewayAPIError decoder := json.NewDecoder(resp.Body) err = decoder.Decode(&error) if err != nil { return err } error.StatusCode = resp.StatusCode error.Debug() return error } else if securityGroupsDelete != "" { resp, err := cmd.API.DeleteResponse(fmt.Sprintf("security_groups/%s", securityGroupsDelete)) if err != nil { return err } defer resp.Body.Close() // Succeed PUT code if resp.StatusCode == 204 { return nil } var error api.ScalewayAPIError decoder := json.NewDecoder(resp.Body) err = decoder.Decode(&error) if err != nil { return err } error.StatusCode = resp.StatusCode error.Debug() return error } else if securityGroupsRules != "" { securityGroupRules, err := cmd.API.GetSecurityGroupRules(securityGroupsRules) if err != nil { return err } printRawMode(cmd.Streams().Stdout, *securityGroupRules) return nil } else if securityGroupsRuleID != "" { newParts := strings.SplitN(securityGroupsRuleID, ":", 2) if len(newParts) != 2 { return cmd.PrintShortUsage() } GroupRuleID, err := cmd.API.GetASecurityGroupRule(newParts[0], newParts[1]) if err != nil { return err } printRawMode(cmd.Streams().Stdout, *GroupRuleID) return nil } else if securityGroupsRuleDelete != "" { newParts := strings.SplitN(securityGroupsRuleDelete, ":", 2) if len(newParts) != 2 { return cmd.PrintShortUsage() } resp, err := cmd.API.DeleteResponse(fmt.Sprintf("security_groups/%s/rules/%s", newParts[0], newParts[1])) if err != nil { return err } defer resp.Body.Close() // Succeed PUT code if resp.StatusCode == 204 { return nil } var error api.ScalewayAPIError decoder := json.NewDecoder(resp.Body) err = decoder.Decode(&error) if err != nil { return err } error.StatusCode = resp.StatusCode error.Debug() return error } else if securityGroupsRuleNew != "" { var newRule api.ScalewayNewSecurityGroupRule newParts := strings.Split(securityGroupsRuleNew, ":") if len(newParts) != 5 && len(newParts) != 6 { return cmd.PrintShortUsage() } newRule.Action = newParts[1] newRule.Direction = newParts[2] newRule.IPRange = newParts[3] newRule.Protocol = newParts[4] if len(newParts) == 6 { var err error newRule.DestPortFrom, err = strconv.Atoi(newParts[5]) if err != nil { return err } } resp, err := cmd.API.PostResponse(fmt.Sprintf("security_groups/%s/rules", newParts[0]), newRule) if err != nil { return err } defer resp.Body.Close() // Succeed POST code if resp.StatusCode == 201 { return nil } var error api.ScalewayAPIError decoder := json.NewDecoder(resp.Body) err = decoder.Decode(&error) if err != nil { return err } error.StatusCode = resp.StatusCode error.Debug() return error } else if securityGroupsRuleUpdate != "" { var newRule api.ScalewayNewSecurityGroupRule newParts := strings.Split(securityGroupsRuleUpdate, ":") if len(newParts) != 6 && len(newParts) != 7 { return cmd.PrintShortUsage() } newRule.Action = newParts[2] newRule.Direction = newParts[3] newRule.IPRange = newParts[4] newRule.Protocol = newParts[5] if len(newParts) == 7 { var err error newRule.DestPortFrom, err = strconv.Atoi(newParts[6]) if err != nil { return err } } resp, err := cmd.API.PutResponse(fmt.Sprintf("security_groups/%s/rules/%s", newParts[0], newParts[1]), newRule) if err != nil { return err } defer resp.Body.Close() // Succeed PUT code if resp.StatusCode == 200 { return nil } var error api.ScalewayAPIError decoder := json.NewDecoder(resp.Body) err = decoder.Decode(&error) if err != nil { return err } error.StatusCode = resp.StatusCode error.Debug() return error } if len(args) == 1 { securityGroups, err := cmd.API.GetASecurityGroup(args[0]) if err != nil { return err } printRawMode(cmd.Streams().Stdout, *securityGroups) return nil } securityGroups, err := cmd.API.GetSecurityGroups() if err != nil { return err } printRawMode(cmd.Streams().Stdout, *securityGroups) return nil }