Cloudflare createdomain (#113)

* fixing #112

* fixing build issue

* typo

* adding missing commits from master

* fixing gofmt validation errors

* fixing github pull request sugestions

* fixing build issue

* adding debug info

* adding more debug

* tweaking code based on pull request comments

* fixing error
This commit is contained in:
Tiernan OToole 2017-05-05 22:20:43 +01:00 committed by Craig Peterson
parent d98c57c85f
commit 5cad1c3513
3 changed files with 38 additions and 2 deletions

View file

@ -49,9 +49,9 @@ D("example.tld", REG_NAMECOM, DnsProvider(CFLARE),
DNSControl depends on a Cloudflare Global API Key that's available under "My Settings".
## New domains
If a domain does not exist in your CloudFlare account, DNSControl
will *not* automatically add it. You'll need to do that via the
control panel manually.
control panel manually or via the command `dnscontrol create-domains`
-command.

View file

@ -227,6 +227,11 @@ func newCloudflare(m map[string]string, metadata json.RawMessage) (providers.DNS
return nil, fmt.Errorf("Cloudflare apikey and apiuser must be provided.")
}
err := api.fetchDomainList()
if err != nil {
return nil, err
}
if len(metadata) > 0 {
parsedMeta := &struct {
IPConversions string `json:"ip_conversions"`
@ -300,3 +305,13 @@ func getProxyMetadata(r *models.RecordConfig) map[string]string {
"proxy": fmt.Sprint(proxied),
}
}
func (c *CloudflareApi) EnsureDomainExists(domain string) error {
if _, ok := c.domainIndex[domain]; ok {
return nil
}
var id string
id, err := c.createZone(domain)
fmt.Printf("Added zone for %s to Cloudflare account: %s\n", domain, id)
return err
}

View file

@ -88,6 +88,27 @@ func (c *CloudflareApi) deleteRec(rec *cfRecord, domainID string) *models.Correc
}
}
func (c *CloudflareApi) createZone(domainName string) (string, error) {
type createZone struct {
Name string `json:"name"`
}
var id string
cz := &createZone{
Name: domainName}
buf := &bytes.Buffer{}
encoder := json.NewEncoder(buf)
if err := encoder.Encode(cz); err != nil {
return "", err
}
req, err := http.NewRequest("POST", zonesURL, buf)
if err != nil {
return "", err
}
c.setHeaders(req)
id, err = handleActionResponse(http.DefaultClient.Do(req))
return id, err
}
func (c *CloudflareApi) createRec(rec *models.RecordConfig, domainID string) []*models.Correction {
type createRecord struct {
Name string `json:"name"`