terraformを使ってconohaでinstance生成 

## はじめに
conohaで色々とお勉強しているけれども、結構な頻度でbreak&buildを行なっている。
あとは専ら土日でやってるのでそろそろGUIじゃなくてMacから自動で構築出来る様にしたい。

以上の理由でterraformを使ってconohaにinstanceを作成することを目標にする！

##### - machine specs
- OS : MacBook
- terraform: v0.11.13

##### - 参考ページ
参考というよりもろぱくり
- [TerraformでConoHaのインスタンスを立ててみる](https://qiita.com/kaminchu/items/d0776c381213d54a3a69)
- [terraformの公式ページ](https://www.terraform.io/docs)

## 結論
実施した内容は本ページの[スタートアップ](https://qiita.com/maki_azuki/items/cf98335a8edaddd0a801#%E3%82%B9%E3%82%BF%E3%83%BC%E3%83%88%E3%82%A2%E3%83%83%E3%83%97)に書いてある通り、９割パクリでいけました。
個人的な収穫は以下の通り。
- セキュリティグループは自前で作る必要がありそう。（全許可/全拒否しかデフォで用意されてない）
- terraformはtfファイルに書かれているresourceをapply/destory共に対象にしてる（と思う）
  - key_pairはぶっちゃけ登録されたままで良かったけどdestory時に無事削除された。

##### - 今後のアクション
- tfファイルをもう少し勉強する。
  - access_keyとかを別ファイル＆変数化
  - セキュリティグループも登録する形に作成する 

##### - 判らなかったこと
- CIとかでは使えそうなのは良く判った。
  - apply & destoryでリソース作成→削除が一貫されている。
- どうやって実運用のプロビジョニングとかで使ってるのかな？ が疑問
  - 前述の通り。仮に複数Node管理でtfファイルをその数分定義してたとして、1node更新(destory→apply)したい場合、他のNodeも巻き込まれて消されると思う。
  - どうやって運用に使っているのかを知りたい

## 事前準備
事前準備はこんな感じ。別にMacじゃなくてもterraformが実行できれば良いと思う

- [Macにterraformをインストール](https://qiita.com/aidy91614/items/6d29c60326907082fc32)
- [conohaのAPIを実行するためのUser/Pass/endpoint情報](https://qiita.com/maki_azuki/items/ca1729e93feb76509f7a#--api%E4%BD%BF%E3%81%86%E3%81%9F%E3%82%81%E3%81%AB%E6%83%85%E5%A0%B1%E3%82%92%E9%9B%86%E3%82%81%E3%82%8B)

## スタートアップ
[TerraformでConoHaのインスタンスを立ててみる](https://qiita.com/kaminchu/items/d0776c381213d54a3a69)を元にど正常ルートの開拓をする。

##### - 方針
- main.tf, flavor/imageはもろぱくり。実行方法も。
  - セキュリティグループは自分で22番ポート用の作成が必要そう。今回は全許可で指定した
    - IPv4的には「gncs-ipv4-all」（全て許可)/「default」（全て拒否）のどちらかだった。 
  - image_nameは定義したimage_nameが「そんなもんない」って言われたのでimage_id指定で実施した

##### - log
実行ログは残しておく
無事、instanceの生成。削除。ssh接続は行えた。

```bash:terraform_init
azuki@maki-mac:~/Deployments/terraform$ terraform init

Initializing provider plugins...
- Checking for available provider plugins on https://releases.hashicorp.com...
- Downloading plugin for provider "openstack" (1.17.0)...

The following providers do not have any version constraints in configuration,
so the latest version was installed.

To prevent automatic upgrades to new major versions that may contain breaking
changes, it is recommended to add version = "..." constraints to the
corresponding provider blocks in configuration, with the constraint strings
suggested below.

* provider.openstack: version = "~> 1.17"

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
azuki@maki-mac:~/Deployments/terraform$ echo $?
0
azuki@maki-mac:~/Deployments/terraform$
```

```bash:terrafomr_apply

azuki@maki-mac:~/Deployments/terraform$ terraform apply

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  + openstack_compute_instance_v2.basic
      id:                         <computed>
      access_ip_v4:               <computed>
      access_ip_v6:               <computed>
      all_metadata.%:             <computed>
      availability_zone:          <computed>
      flavor_id:                  <computed>
      flavor_name:                "g-512mb"
      force_delete:               "false"
      image_id:                   "de03b36d-21d8-4e58-9b8e-24581dda86fa"
      image_name:                 <computed>
      key_pair:                   "terraform-keypair"
      metadata.%:                 "1"
      metadata.instance_name_tag: "azuki-test"
      name:                       "basic"
      network.#:                  <computed>
      power_state:                "active"
      region:                     <computed>
      security_groups.#:          "1"
      security_groups.645319369:  "gncs-ipv4-all"
      stop_before_destroy:        "false"

  + openstack_compute_keypair_v2.keypair
      id:                         <computed>
      fingerprint:                <computed>
      name:                       "terraform-keypair"
      private_key:                <computed>
      public_key:                 "ssh-rsa XXXXXXX@YYYYYY"
      region:                     <computed>


Plan: 2 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

openstack_compute_keypair_v2.keypair: Creating...
  fingerprint: "" => "<computed>"
  name:        "" => "terraform-keypair"
  private_key: "" => "<computed>"
  public_key:  "" => "ssh-rsa XXXXXXX@YYYYYY"
  region:      "" => "<computed>"
openstack_compute_instance_v2.basic: Creating...
  access_ip_v4:               "" => "<computed>"
  access_ip_v6:               "" => "<computed>"
  all_metadata.%:             "" => "<computed>"
  availability_zone:          "" => "<computed>"
  flavor_id:                  "" => "<computed>"
  flavor_name:                "" => "g-512mb"
  force_delete:               "" => "false"
  image_id:                   "" => "de03b36d-21d8-4e58-9b8e-24581dda86fa"
  image_name:                 "" => "<computed>"
  key_pair:                   "" => "terraform-keypair"
  metadata.%:                 "" => "1"
  metadata.instance_name_tag: "" => "azuki-test"
  name:                       "" => "basic"
  network.#:                  "" => "<computed>"
  power_state:                "" => "active"
  region:                     "" => "<computed>"
  security_groups.#:          "" => "1"
  security_groups.645319369:  "" => "gncs-ipv4-all"
  stop_before_destroy:        "" => "false"
openstack_compute_keypair_v2.keypair: Creation complete after 1s (ID: terraform-keypair)
openstack_compute_instance_v2.basic: Still creating... (10s elapsed)
openstack_compute_instance_v2.basic: Still creating... (20s elapsed)
openstack_compute_instance_v2.basic: Creation complete after 23s (ID: 075fd2d1-2bd8-4e65-9314-eb020ee50929)

Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
azuki@maki-mac:~/Deployments/terraform$
```

```bash:ssh接続

azuki@maki-mac:~/Deployments/terraform$ ssh root@118.27.39.152
The authenticity of host '118.27.39.152 (118.27.39.152)' can't be
~snip~
root@118-27-39-152:~# 
```

```bash:terraform_destroy
azuki@maki-mac:~/Deployments/terraform$ terraform destroy
openstack_compute_instance_v2.basic: Refreshing state... (ID: 075fd2d1-2bd8-4e65-9314-eb020ee50929)
openstack_compute_keypair_v2.keypair: Refreshing state... (ID: terraform-keypair)

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  - destroy

Terraform will perform the following actions:

  - openstack_compute_instance_v2.basic

  - openstack_compute_keypair_v2.keypair


Plan: 0 to add, 0 to change, 2 to destroy.

Do you really want to destroy all resources?
  Terraform will destroy all your managed infrastructure, as shown above.
  There is no undo. Only 'yes' will be accepted to confirm.

  Enter a value: yes

openstack_compute_keypair_v2.keypair: Destroying... (ID: terraform-keypair)
openstack_compute_instance_v2.basic: Destroying... (ID: 075fd2d1-2bd8-4e65-9314-eb020ee50929)
openstack_compute_keypair_v2.keypair: Destruction complete after 0s
openstack_compute_instance_v2.basic: Still destroying... (ID: 075fd2d1-2bd8-4e65-9314-eb020ee50929, 10s elapsed)
openstack_compute_instance_v2.basic: Destruction complete after 12s

Destroy complete! Resources: 2 destroyed.
azuki@maki-mac:~/Deployments/terraform$
```