Cloudflare Partial (CNAME) setup を API で行う

# Partial (CNAME) setup とは

こちらをご参照ください。

- [Cloudflare の DNS セットアップ方式まとめ - Qiita](https://qiita.com/khayama/items/13f441abb9ba1b870db7#partial-cname-setup)

# API で行う必要性

通常アカウントでは、[Business もしくは Enterprise プランのゾーンにのみ Partial (CNAME) setup が許可](https://developers.cloudflare.com/dns/zone-setups/partial-setup/#availability)されていますが、

Cloudflare パートナーアカウントでは、Free プランを含む全てのプランで Partial (CNAME) setup が許可されています。

ただし、**GUI による Partial (CNAME) setup は Business もしくは Enterprise プランのゾーンのみ**、のままになっています。

> https://dash.cloudflare.com/?to=/:account/:zone
>
> Business もしくは Enterprise プラン
>
> ![image-20220407011242602](https://cdn.jsdelivr.net/gh/kyouheicf/image@master/uPic/image-20220407011242602.png)
>
> Free もしくは Pro プラン
>
> ![image-20220407011146086](https://cdn.jsdelivr.net/gh/kyouheicf/image@master/uPic/image-20220407011146086.png)

# API で Partial (CNAME) setup ゾーン作成

変数セットして、API からゾーン作成します。

```shell
export EMAIL='YOUR_EMAIL'
export APIKEY='YOUR_APIKEY'
export ACCOUNT_ID='YOUR_ACCOUNT_ID'
export ZONE_NAME='YOUR_ZONE_NAME'
```

`ZONE_NAME` と `type: partial` を指定してゾーン作成します（作成時にプラン指定はできません）。

```shell
curl -sX POST "https://api.cloudflare.com/client/v4/zones" \
     -H "X-Auth-Email: $EMAIL" \
     -H "X-Auth-Key: $APIKEY" \
     -H "Content-Type: application/json" \
     --data '{"name":"'"$ZONE_NAME"'","account":{"id":"'"$ACCOUNT_ID"'"},"jump_start":false,"type":"partial"}' | jq
```

Free プランで Partial (CNAME) setup ゾーンが作成できました。

その後のプラン変更は [Cloudflare のゾーン削除・プラン変更 - Qiita](https://qiita.com/khayama/items/197a4fa124680345fc63#%E3%83%97%E3%83%A9%E3%83%B3%E5%A4%89%E6%9B%B4) をご参照ください。

```json:result.json
{
  "result": {
    "id": "xxx",
    "name": "example.com",
    "status": "pending",
    "paused": false,
    "type": "partial",
    "development_mode": 0,
    "verification_key": "xxx-xxx",
    "cname_suffix": "cdn.cloudflare.net",
    "original_name_servers": [
      "ns1.xxx.com",
      "ns2.xxx.com"
    ],
    "original_registrar": "xxx",
    "original_dnshost": null,
    "modified_on": "2021-10-11T03:41:49.595275Z",
    "created_on": "2021-10-11T03:41:49.595275Z",
    "activated_on": null,
    "meta": {
      "step": 4,
      "wildcard_proxiable": false,
      "custom_certificate_quota": 0,
      "page_rule_quota": 3,
      "phishing_detected": false,
      "multiple_railguns_allowed": false
    },
    "owner": {
      "id": "xxx",
      "type": "organization",
      "name": "xxx Account"
    },
    "account": {
      "id": "xxx",
      "name": "xxx Account"
    },
    "permissions": [
      "#access:edit",
      "#access:read",
      "#analytics:read",
      "#app:edit",
      "#auditlogs:read",
      "#billing:edit",
      "#billing:read",
      "#cache_purge:edit",
      "#dns_records:edit",
      "#dns_records:read",
      "#lb:edit",
      "#lb:read",
      "#legal:edit",
      "#legal:read",
      "#logs:edit",
      "#logs:read",
      "#member:edit",
      "#member:read",
      "#organization:edit",
      "#organization:read",
      "#ssl:edit",
      "#ssl:read",
      "#stream:edit",
      "#stream:read",
      "#subscription:edit",
      "#subscription:read",
      "#teams:edit",
      "#teams:pii",
      "#teams:read",
      "#teams:report",
      "#waf:edit",
      "#waf:read",
      "#webhooks:edit",
      "#webhooks:read",
      "#worker:edit",
      "#worker:read",
      "#zone:edit",
      "#zone:read",
      "#zone_settings:edit",
      "#zone_settings:read"
    ],
    "plan": {
      "id": "0feeeeeeeeeeeeeeeeeeeeeeeeeeeeee",
      "name": "Free Website",
      "price": 0,
      "currency": "USD",
      "frequency": "",
      "is_subscribed": false,
      "can_subscribe": false,
      "legacy_id": "free",
      "legacy_discount": false,
      "externally_managed": false
    }
  },
  "success": true,
  "errors": [],
  "messages": []
}
```