Skip to content

Commit

Permalink
Merge pull request #10 from SoftIron/sg_changes
Browse files Browse the repository at this point in the history
fix: explicit fields for CreateSecurityGroupRequest
  • Loading branch information
masonkatz authored May 30, 2024
2 parents b2e52a3 + c2ff4d4 commit 19a5612
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 10 deletions.
16 changes: 11 additions & 5 deletions cloud/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ type UpdateGroupRequest struct {
}

// SetTemplate sets r.Template from its structured representation. Either call
// this of set the template string directly.
// this or set the template string directly.
func (r *UpdateGroupRequest) SetTemplate(t GroupTemplate) {
r.Template = mustParseTemplate(t).String()
}
Expand Down Expand Up @@ -226,7 +226,7 @@ type CreateImageRequest struct {
}

// SetTemplate sets r.Template from its structured representation. Either call
// this of set the template string directly.
// this or set the template string directly.
func (r *CreateImageRequest) SetTemplate(t ImageTemplate) {
r.Template = mustParseTemplate(t).String()
}
Expand Down Expand Up @@ -259,7 +259,7 @@ type UpdateImageRequest struct {
}

// SetTemplate sets r.Template from its structured representation. Either call
// this of set the template string directly.
// this or set the template string directly.
func (r *UpdateImageRequest) SetTemplate(t ImageTemplate) {
r.Template = mustParseTemplate(t).String()
}
Expand Down Expand Up @@ -297,7 +297,7 @@ type CreateInstanceRequest struct {
}

// SetTemplate sets r.Template from its structured representation. Either call
// this of set the template string directly.
// this or set the template string directly.
func (r *CreateInstanceRequest) SetTemplate(t InstanceTemplate) {
r.Template = mustParseTemplate(t).String()
}
Expand Down Expand Up @@ -437,6 +437,12 @@ type CreateSecurityGroupRequest struct {
Template string `json:"template"`
}

// SetTemplate sets r.Template from its structured representation. Either call
// this or set the template string directly.
func (r *CreateSecurityGroupRequest) SetTemplate(t SecurityGroupTemplate) {
r.Template = NewTemplate(t).String()
}

// CloneSecurityGroupRequest is the request body for POST /cloud/security-group/{sg}/clone.
type CloneSecurityGroupRequest struct {
Name string `json:"name"`
Expand Down Expand Up @@ -553,7 +559,7 @@ type UpdateUserRequest struct {
}

// SetTemplate sets r.Template from its structured representation. Either call
// this of set the template string directly.
// this or set the template string directly.
func (r *UpdateUserRequest) SetTemplate(t UserTemplate) {
r.Template = mustParseTemplate(t).String()
}
Expand Down
5 changes: 2 additions & 3 deletions cloud/service_sg.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func (s SecurityGroupService) DeleteSecurityGroup(ctx context.Context, id int) e
return s.Delete(ctx, p, nil)
}

// SecurityGroup returns information about a image.
// SecurityGroup returns information about a security group.
func (s SecurityGroupService) SecurityGroup(ctx context.Context, id int) (*SecurityGroupResponse, error) {
var resp SecurityGroupResponse

Expand All @@ -29,7 +29,7 @@ func (s SecurityGroupService) SecurityGroup(ctx context.Context, id int) (*Secur
return &resp, nil
}

// SecurityGroups returns information about all images.
// SecurityGroups returns information about all security groups.
func (s SecurityGroupService) SecurityGroups(ctx context.Context) (*SecurityGroupsResponse, error) {
var resp SecurityGroupsResponse

Expand Down Expand Up @@ -110,7 +110,6 @@ func (s SecurityGroupService) RenameSecurityGroup(ctx context.Context, id int, r
// CreateSecurityGroup creates a new security group.
func (s SecurityGroupService) CreateSecurityGroup(ctx context.Context, req CreateSecurityGroupRequest) (*CreateSecurityGroupResponse, error) {
var resp CreateSecurityGroupResponse

p := s.path(SecurityGroupPath)

if err := s.Post(ctx, p, req, &resp); err != nil {
Expand Down
22 changes: 20 additions & 2 deletions cloud/sg.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,20 @@ type SecurityGroup struct {

// SecurityGroupTemplate is the API payload based on the legacy xmlrpc backend.
type SecurityGroupTemplate struct {
Name string
Description string
Rule []SecurityGroupRule
}

// SecurityGroupRule is the API payload based on the legacy xmlrpc backend.
type SecurityGroupRule struct {
Protocol string `json:"protocol" yaml:"protocol"`
RuleType string `json:"rule_type" yaml:"rule_type"`
ICMPType string `json:"icmp_type" yaml:"icmp_type"`
IP string `json:"ip" yaml:"ip"`
NetworkID string `json:"network_id" yaml:"network_id"`
Protocol string `json:"protocol" yaml:"protocol"`
Range string `json:"range" yaml:"range"`
RuleType string `json:"rule_type" yaml:"rule_type"`
Size string `json:"size" yaml:"size"`
}

// ParseTemplate returns a structured subset of the nested key x value pair map.
Expand All @@ -38,6 +44,10 @@ func (g *SecurityGroup) ParseTemplate() (*SecurityGroupTemplate, error) {
if key == "DESCRIPTION" {
t.Description = v
}

if key == "NAME" {
t.Name = v
}
case map[string]any:
if key == "RULE" {
t.Rule = []SecurityGroupRule{*newSecurityGroupRule(v)}
Expand All @@ -64,6 +74,14 @@ func newSecurityGroupRule(m map[string]any) *SecurityGroupRule {
r.Protocol = v
case "RULE_TYPE":
r.RuleType = v
case "IP":
r.IP = v
case "RANGE":
r.Range = v
case "ICMP_TYPE":
r.ICMPType = v
case "NETWORK_ID":
r.NetworkID = v
}
}
}
Expand Down

0 comments on commit 19a5612

Please sign in to comment.