-
Notifications
You must be signed in to change notification settings - Fork 138
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
adds ovh_iploadbalancing_tcp_farm_server resource #33
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,234 @@ | ||
package ovh | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"log" | ||
"net" | ||
|
||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
type IpLoadbalancingTcpFarmServer struct { | ||
BackendId int `json:"backendId,omitempty"` | ||
ServerId int `json:"serverId,omitempty"` | ||
FarmId int `json:"farmId,omitempty"` | ||
DisplayName *string `json:"displayName,omitempty"` | ||
Address *string `json:"address"` | ||
Cookie *string `json:"cookie,omitempty"` | ||
Port *int `json:"port"` | ||
ProxyProtocolVersion *string `json:"proxyProtocolVersion"` | ||
Chain *string `json:"chain"` | ||
Weight *int `json:"weight"` | ||
Probe *bool `json:"probe"` | ||
Ssl *bool `json:"ssl"` | ||
Backup *bool `json:"backup"` | ||
Status *string `json:"status"` | ||
} | ||
|
||
func resourceIpLoadbalancingTcpFarmServer() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceIpLoadbalancingTcpFarmServerCreate, | ||
Read: resourceIpLoadbalancingTcpFarmServerRead, | ||
Update: resourceIpLoadbalancingTcpFarmServerUpdate, | ||
Delete: resourceIpLoadbalancingTcpFarmServerDelete, | ||
Schema: map[string]*schema.Schema{ | ||
"service_name": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"farm_id": &schema.Schema{ | ||
Type: schema.TypeInt, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"display_name": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
"address": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) { | ||
ip := v.(string) | ||
if net.ParseIP(ip).To4() == nil { | ||
errors = append(errors, fmt.Errorf("Address %s is not an IPv4", ip)) | ||
} | ||
return | ||
}, | ||
}, | ||
"ssl": &schema.Schema{ | ||
Type: schema.TypeBool, | ||
Optional: true, | ||
Default: false, | ||
}, | ||
"cookie": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"port": &schema.Schema{ | ||
Type: schema.TypeInt, | ||
Optional: true, | ||
}, | ||
"proxy_protocol_version": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) { | ||
err := validateStringEnum(v.(string), []string{"v1", "v2", "v2-ssl", "v2-ssl-cn"}) | ||
if err != nil { | ||
errors = append(errors, err) | ||
} | ||
return | ||
}, | ||
}, | ||
"chain": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
"weight": &schema.Schema{ | ||
Type: schema.TypeInt, | ||
Optional: true, | ||
Default: 1, | ||
}, | ||
"probe": &schema.Schema{ | ||
Type: schema.TypeBool, | ||
Optional: true, | ||
Default: false, | ||
}, | ||
"backup": &schema.Schema{ | ||
Type: schema.TypeBool, | ||
Optional: true, | ||
Default: false, | ||
}, | ||
"status": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) { | ||
err := validateStringEnum(v.(string), []string{"active", "inactive"}) | ||
if err != nil { | ||
errors = append(errors, err) | ||
} | ||
return | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceIpLoadbalancingTcpFarmServerCreate(d *schema.ResourceData, meta interface{}) error { | ||
config := meta.(*Config) | ||
|
||
newBackendServer := &IpLoadbalancingTcpFarmServer{ | ||
DisplayName: getNilStringPointer(d.Get("display_name").(string)), | ||
Address: getNilStringPointer(d.Get("address").(string)), | ||
Port: getNilIntPointer(d.Get("port").(int)), | ||
ProxyProtocolVersion: getNilStringPointer(d.Get("proxy_protocol_version").(string)), | ||
Chain: getNilStringPointer(d.Get("chain").(string)), | ||
Weight: getNilIntPointer(d.Get("weight").(int)), | ||
Probe: getNilBoolPointer(d.Get("probe").(bool)), | ||
Ssl: getNilBoolPointer(d.Get("ssl").(bool)), | ||
Backup: getNilBoolPointer(d.Get("backup").(bool)), | ||
Status: getNilStringPointer(d.Get("status").(string)), | ||
} | ||
|
||
service := d.Get("service_name").(string) | ||
farmid := d.Get("farm_id").(int) | ||
r := &IpLoadbalancingTcpFarmServer{} | ||
endpoint := fmt.Sprintf("/ipLoadbalancing/%s/tcp/farm/%d/server", service, farmid) | ||
|
||
err := config.OVHClient.Post(endpoint, newBackendServer, r) | ||
if err != nil { | ||
return fmt.Errorf("calling %s with %d:\n\t %s", endpoint, farmid, err.Error()) | ||
} | ||
|
||
//set id | ||
d.SetId(fmt.Sprintf("%d", r.ServerId)) | ||
|
||
return nil | ||
} | ||
|
||
func resourceIpLoadbalancingTcpFarmServerRead(d *schema.ResourceData, meta interface{}) error { | ||
config := meta.(*Config) | ||
|
||
service := d.Get("service_name").(string) | ||
farmid := d.Get("farm_id").(int) | ||
r := &IpLoadbalancingTcpFarmServer{} | ||
|
||
endpoint := fmt.Sprintf("/ipLoadbalancing/%s/tcp/farm/%d/server/%s", service, farmid, d.Id()) | ||
|
||
err := config.OVHClient.Get(endpoint, r) | ||
if err != nil { | ||
return fmt.Errorf("calling %s :\n\t %q", endpoint, err) | ||
} | ||
log.Printf("[DEBUG] Response object from OVH : %v", r) | ||
|
||
d.Set("probe", *r.Probe) | ||
d.Set("ssl", *r.Ssl) | ||
d.Set("backup", *r.Backup) | ||
d.Set("address", *r.Address) | ||
if r.DisplayName != nil { | ||
d.Set("display_name", *r.DisplayName) | ||
} | ||
if r.Cookie != nil { | ||
d.Set("cookie", *r.Cookie) | ||
} | ||
d.Set("port", *r.Port) | ||
if r.ProxyProtocolVersion != nil { | ||
d.Set("proxy_protocol_version", *r.ProxyProtocolVersion) | ||
} | ||
if r.Chain != nil { | ||
d.Set("chain", *r.Chain) | ||
} | ||
d.Set("weight", *r.Weight) | ||
d.Set("status", *r.Status) | ||
|
||
return nil | ||
} | ||
|
||
func resourceIpLoadbalancingTcpFarmServerUpdate(d *schema.ResourceData, meta interface{}) error { | ||
config := meta.(*Config) | ||
|
||
update := &IpLoadbalancingTcpFarmServer{ | ||
DisplayName: getNilStringPointer(d.Get("display_name").(string)), | ||
Address: getNilStringPointer(d.Get("address").(string)), | ||
Port: getNilIntPointer(d.Get("port").(int)), | ||
ProxyProtocolVersion: getNilStringPointer(d.Get("proxy_protocol_version").(string)), | ||
Chain: getNilStringPointer(d.Get("chain").(string)), | ||
Weight: getNilIntPointer(d.Get("weight").(int)), | ||
Probe: getNilBoolPointer(d.Get("probe").(bool)), | ||
Ssl: getNilBoolPointer(d.Get("ssl").(bool)), | ||
Backup: getNilBoolPointer(d.Get("backup").(bool)), | ||
Status: getNilStringPointer(d.Get("status").(string)), | ||
} | ||
|
||
service := d.Get("service_name").(string) | ||
farmid := d.Get("farm_id").(int) | ||
r := &IpLoadbalancingTcpFarmServer{} | ||
endpoint := fmt.Sprintf("/ipLoadbalancing/%s/tcp/farm/%d/server/%s", service, farmid, d.Id()) | ||
js, _ := json.Marshal(update) | ||
log.Printf("[DEBUG] PUT %s : %v", endpoint, string(js)) | ||
err := config.OVHClient.Put(endpoint, update, r) | ||
if err != nil { | ||
return fmt.Errorf("calling %s with %d:\n\t %s", endpoint, farmid, err.Error()) | ||
} | ||
return nil | ||
} | ||
|
||
func resourceIpLoadbalancingTcpFarmServerDelete(d *schema.ResourceData, meta interface{}) error { | ||
config := meta.(*Config) | ||
|
||
service := d.Get("service_name").(string) | ||
farmid := d.Get("farm_id").(int) | ||
|
||
r := &IpLoadbalancingTcpFarmServer{} | ||
endpoint := fmt.Sprintf("/ipLoadbalancing/%s/tcp/farm/%d/server/%s", service, farmid, d.Id()) | ||
|
||
err := config.OVHClient.Delete(endpoint, r) | ||
if err != nil { | ||
return fmt.Errorf("calling %s :\n\t %s", endpoint, err.Error()) | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i don't see the use of "getNilStringPointer"
AFAIK, there's no such thing elswhere in terraform providers. what are we trying to do here ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assuming "display_name" is not set,
d.Get("display_name").(string)
returns an empty string""
, but a string none the less. gosjson.Marshal
method correctly interprets it as an empty string rather then a not set string. This is why the struct uses pointers instead of string values, so they can havenil
value. getNilStringPointer then gets the string value and turns it into a pointer to string value, that in case oflen(<string>) == 0
returns nil, so thatjson.Marshal()
instead of generating"displayName": ""
generates"displayName": null
which OVH API expects (ie. it will complain if you will have a zero value field instead of null for attributes that can not be changed after resource creationThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
have you tried with d.Get("display_name").(*string) ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in fact I did :) it did not work. Don't think type assertion is meant to work like that at all, and even if it did, it would have no way to distinct between nil and zero value, so instead of recovering
nil
, it would recover*""
but as I said, it's not doing even that.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the functions I provided here do just a bit more then what you can see in pointer retrievals for aws like :
https://github.com/terraform-providers/terraform-provider-aws/blob/80e4ae53941c6fc87239f1d704e2dfe6a4dec3b6/aws/resource_aws_mq_broker.go#L185
https://github.com/aws/aws-sdk-go/blob/71a68668c287f9ad58d0c58c78e27f08eb39fa49/aws/convert_types.go#L5
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok then