func testAccCheckComputeV2KeypairExists(t *testing.T, n string, kp *keypairs.KeyPair) resource.TestCheckFunc {
	return func(s *terraform.State) error {
		rs, ok := s.RootModule().Resources[n]
		if !ok {
			return fmt.Errorf("Not found: %s", n)
		}

		if rs.Primary.ID == "" {
			return fmt.Errorf("No ID is set")
		}

		config := testAccProvider.Meta().(*Config)
		computeClient, err := config.computeV2Client(OS_REGION_NAME)
		if err != nil {
			return fmt.Errorf("(testAccCheckComputeV2KeypairExists) Error creating OpenStack compute client: %s", err)
		}

		found, err := keypairs.Get(computeClient, rs.Primary.ID).Extract()
		if err != nil {
			return err
		}

		if found.Name != rs.Primary.ID {
			return fmt.Errorf("Keypair not found")
		}

		*kp = *found

		return nil
	}
}
Beispiel #2
0
func (c *GenericClient) GetPublicKey(keyPairName string) ([]byte, error) {
	kp, err := keypairs.Get(c.Compute, keyPairName).Extract()
	if err != nil {
		return nil, err
	}
	return []byte(kp.PublicKey), nil
}
Beispiel #3
0
func FindKeyPairByName(cli *gophercloud.ServiceClient, name string) (*keypairs.KeyPair, error) {
	result := keypairs.Get(cli, name)
	keypair, err := result.Extract()
	if err != nil {
		return nil, err
	}
	return keypair, nil
}
func resourceComputeKeypairExists(d *schema.ResourceData, meta interface{}) (bool, error) {
	computeClient, err := meta.(*Config).computeClient()
	if err != nil {
		return false, fmt.Errorf("Error creating ConoHa compute client: %s", err)
	}

	_, err = keypairs.Get(computeClient, d.Id()).Extract()
	if err != nil {
		if code, ok := err.(*gophercloud.UnexpectedResponseCodeError); ok && code.Actual == 404 {
			return false, nil
		}
		return false, err
	}

	return true, nil
}
func resourceComputeKeypairRead(d *schema.ResourceData, meta interface{}) error {
	computeClient, err := meta.(*Config).computeClient()
	if err != nil {
		return fmt.Errorf("Error creating ConoHa compute client: %s", err)
	}

	keypair, err := keypairs.Get(computeClient, d.Id()).Extract()
	if err != nil {
		return err
	}

	d.Set("name", keypair.Name)
	d.Set("public_key", keypair.PublicKey)

	return nil
}
func resourceComputeKeypairV2Read(d *schema.ResourceData, meta interface{}) error {
	config := meta.(*Config)
	computeClient, err := config.computeV2Client(d.Get("region").(string))
	if err != nil {
		return fmt.Errorf("Error creating OpenStack compute client: %s", err)
	}

	kp, err := keypairs.Get(computeClient, d.Id()).Extract()
	if err != nil {
		return CheckDeleted(d, err, "keypair")
	}

	d.Set("name", kp.Name)
	d.Set("public_key", kp.PublicKey)

	return nil
}
func testAccCheckComputeV2KeypairDestroy(s *terraform.State) error {
	config := testAccProvider.Meta().(*Config)
	computeClient, err := config.computeV2Client(OS_REGION_NAME)
	if err != nil {
		return fmt.Errorf("(testAccCheckComputeV2KeypairDestroy) Error creating OpenStack compute client: %s", err)
	}

	for _, rs := range s.RootModule().Resources {
		if rs.Type != "openstack_compute_keypair_v2" {
			continue
		}

		_, err := keypairs.Get(computeClient, rs.Primary.ID).Extract()
		if err == nil {
			return fmt.Errorf("Keypair still exists")
		}
	}

	return nil
}
Beispiel #8
0
// Get returns public data about a previously uploaded KeyPair.
func Get(client *gophercloud.ServiceClient, name string) os.GetResult {
	return os.Get(client, name)
}