Skip to content
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

Scicoria/add eventhub mod #1225

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,8 @@ kubeconfig

# environment files
.env

# transient testing files
ssh_key
ssh_key.pub
*.log
28 changes: 28 additions & 0 deletions NOTES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
NOTES..




# WSL2 issues with DNS and Provider Registration
See these:
https://github.com/hashicorp/terraform-provider-azurerm/issues/15345
https://github.com/golang/go/issues/51127

https://github.com/microsoft/WSL/issues/5420#issuecomment-1248791740
https://github.com/microsoft/WSL/issues/5420

## To workaround
### check the current configurations
cat /etc/resolv.conf

### create the /etc/wsl.conf file to disable /etc/resolv.conf generation
printf "[network]\ngenerateResolvConf = false" >> /etc/wsl.conf

### remove the symlink WSL2 created
rm /etc/resolv.conf

### finally, add your public DNS server to the /etc/resolv.conf file. If your VPN server has a nameserver, add it too
echo "nameserver 8.8.8.8" >> /etc/resolv.conf
echo "nameserver 1.1.1.1" >> /etc/resolv.conf

>Note: the /etc/resolv.conf file gets removed at each reboot
6 changes: 6 additions & 0 deletions examples/azure/terraform-azure-aks-example/output.tf
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
output "client_key" {
value = azurerm_kubernetes_cluster.k8s.kube_config.0.client_key
sensitive = true
}

output "client_certificate" {
value = azurerm_kubernetes_cluster.k8s.kube_config.0.client_certificate
sensitive = true
}

output "cluster_ca_certificate" {
value = azurerm_kubernetes_cluster.k8s.kube_config.0.cluster_ca_certificate
sensitive = true
}

output "cluster_username" {
value = azurerm_kubernetes_cluster.k8s.kube_config.0.username
sensitive = true
}

output "cluster_password" {
value = azurerm_kubernetes_cluster.k8s.kube_config.0.password
sensitive = true
}

output "kube_config" {
Expand All @@ -25,4 +30,5 @@ output "kube_config" {

output "host" {
value = azurerm_kubernetes_cluster.k8s.kube_config.0.host
sensitive = true
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
# ---------------------------------------------------------------------------------------------------------------------

provider "azurerm" {
version = "~> 2.50"
features {}
}

Expand All @@ -21,6 +20,12 @@ terraform {
# 0.12.26 as the minimum version, as that version added support for required_providers with source URLs, making it
# forwards compatible with 0.13.x code.
required_version = ">= 0.12.26"
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~> 2.50"
}
}
}

# ---------------------------------------------------------------------------------------------------------------------
Expand Down
33 changes: 33 additions & 0 deletions examples/azure/terraform-azure-eventhub-example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Terraform Azure Eventhub Example

This folder contains a simple Terraform module that deploys resources in [Azure](https://azure.microsoft.com/) to demonstrate
how you can use Terratest to write automated tests for your Azure Terraform code. This module deploys an [Azure Event Hubs](https://azure.microsoft.com/en-us/products/event-hubs/).

Check out [test/azure/terraform_azure_eventhub_example_test.go](/test/azure/terraform_azure_eventhub_example_test.go) to see how you can write automated tests for this module.

Note that the resources deployed in this module don't actually do anything; it just runs the resources for demonstration purposes.

**WARNING**: This module and the automated tests for it deploy real resources into your Azure account which can cost you money. The resources are all part of the [Azure Free Account](https://azure.microsoft.com/free/), so if you haven't used that up, it should be free, but you are completely responsible for all Azure charges.

## Running this module manually

1. Sign up for [Azure](https://azure.microsoft.com/)
1. Configure your Azure credentials using one of the [supported methods for Azure CLI
tools](https://docs.microsoft.com/cli/azure/azure-cli-configuration?view=azure-cli-latest)
1. Install [Terraform](https://www.terraform.io/) and make sure it's on your `PATH`
1. Ensure [environment variables](../README.md#review-environment-variables) are available
1. Run `terraform init`
1. Run `terraform apply`
1. When you're done, run `terraform destroy`

## Running automated tests against this module

1. Sign up for [Azure](https://azure.microsoft.com/).
2. Configure your Azure credentials using one of the [supported methods for Azure CLI tools](https://docs.microsoft.com/cli/azure/azure-cli-configuration?view=azure-cli-latest).

3. Install [Terraform](https://www.terraform.io/) and make sure it's on your `PATH`.
4. [Review environment variables](#review-environment-variables).
5. Install [Golang](https://golang.org/) and make sure this code is checked out into your `GOPATH`.
6. `cd test`
7. Make sure [the azure-sdk-for-go versions match](#check-go-dependencies) in [/test/go.mod](/test/go.mod) and in [test/azure/terraform_azure_eventhub_example_test.go](/test/azure/terraform_azure_eventhub_example_test.go).
8. `go test -v -run TestTerraformAzureEventhubkExample`
47 changes: 47 additions & 0 deletions examples/azure/terraform-azure-eventhub-example/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# ---------------------------------------------------------------------------------------------------------------------
# DEPLOY AN AZURE EVENT HUB
# This is an example of how to deploy a event hub.
# ---------------------------------------------------------------------------------------------------------------------
# See test/azure/terraform_azure_eventhub_example_test.go for how to write automated tests for this code.
# ---------------------------------------------------------------------------------------------------------------------

provider "azurerm" {
features {}
}

terraform {
# This module is now only being tested with Terraform 0.13.x. However, to make upgrading easier, we are setting
# 0.12.26 as the minimum version, as that version added support for required_providers with source URLs, making it
# forwards compatible with 0.13.x code.
required_version = ">= 0.12.26"
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~> 2.29"
}
}
}

# ---------------------------------------------------------------------------------------------------------------------
# DEPLOY A RESOURCE GROUP
# ---------------------------------------------------------------------------------------------------------------------

resource "azurerm_resource_group" "eventhub_rg" {
name = "terratest-eventhub-rg-${var.postfix}"
location = var.location
}

resource "azurerm_eventhub_namespace" "eventhub_namespace" {
name = "terratest-eventhub-namespace-${var.postfix}"
location = azurerm_resource_group.eventhub_rg.location
resource_group_name = azurerm_resource_group.eventhub_rg.name
sku = "Standard"
}

resource "azurerm_eventhub" "eventhub" {
name = "terratest-eventhub-${var.postfix}"
namespace_name = azurerm_eventhub_namespace.eventhub_namespace.name
resource_group_name = azurerm_resource_group.eventhub_rg.name
partition_count = 2
message_retention = 1
}
19 changes: 19 additions & 0 deletions examples/azure/terraform-azure-eventhub-example/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
output "resource_group_name" {
value = azurerm_resource_group.main.name
}

output "eventhub_namespace_name" {
value = azurerm_eventhub_namespace.main.name
}

output "eventhub_name" {
value = azurerm_eventhub.main.name
}

output "eventhub_partition_count" {
value = azurerm_eventhub.main.partition_count
}

output "eventhub_message_retention" {
value = azurerm_eventhub.main.message_retention
}
11 changes: 11 additions & 0 deletions examples/azure/terraform-azure-eventhub-example/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
variable "postfix" {
description = "A postfix string to centrally mitigate resource name collisions"
type = string
default = "resource"
}

variable "location" {
description = "The Azure region in which to deploy your resources to"
type = string
default = "East US"
}
7 changes: 6 additions & 1 deletion examples/azure/terraform-azure-example/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
# ---------------------------------------------------------------------------------------------------------------------

provider "azurerm" {
version = "~> 2.50"
features {}
}

Expand All @@ -21,6 +20,12 @@ terraform {
# 0.12.26 as the minimum version, as that version added support for required_providers with source URLs, making it
# forwards compatible with 0.13.x code.
required_version = ">= 0.12.26"
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~> 2.50"
}
}
}

# ---------------------------------------------------------------------------------------------------------------------
Expand Down
6 changes: 5 additions & 1 deletion examples/azure/terraform-azure-postgresql-example/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
# CONFIGURE OUR AZURE CONNECTION
# ---------------------------------------------------------------------------------------------------------------------
provider "azurerm" {
features {}
features {
resource_group {
prevent_deletion_if_contains_resources = false
}
}
}

# ---------------------------------------------------------------------------------------------------------------------
Expand Down
7 changes: 6 additions & 1 deletion examples/azure/terraform-azure-vm-example/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
# ---------------------------------------------------------------------------------------------------------------------

provider "azurerm" {
version = "~> 2.50"
features {}
}

Expand All @@ -20,6 +19,12 @@ terraform {
# 0.12.26 as the minimum version, as that version added support for required_providers with source URLs, making it
# forwards compatible with 0.13.x code.
required_version = ">= 0.12.26"
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~> 2.50"
}
}
}

# ---------------------------------------------------------------------------------------------------------------------
Expand Down
55 changes: 55 additions & 0 deletions modules/azure/eventhub.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package azure

import (
"context"

//"github.com/Azure/azure-sdk-for-go/services/postgresql/mgmt/2017-12-01/postgresql"
"github.com/Azure/azure-sdk-for-go/services/eventhub/mgmt/2017-04-01/eventhub"
"github.com/Azure/azure-sdk-for-go/services/postgresql/mgmt/2017-12-01/postgresql"
"github.com/gruntwork-io/terratest/modules/testing"
"github.com/stretchr/testify/require"
)

// GetPostgreSQLServerClientE is a helper function that will setup a postgresql server client.
func GetEventHubClientE(subscriptionID string) (*eventhub.EventHubsClient, error) {
// Validate Azure subscription ID
subscriptionID, err := getTargetAzureSubscription(subscriptionID)
if err != nil {
return nil, err
}

// Create a postgresql server client
eventHubClient := eventhub.NewEventHubsClient(subscriptionID)

// Create an authorizer
authorizer, err := NewAuthorizer()
if err != nil {
return nil, err
}

// Attach authorizer to the client
eventHubClient.Authorizer = *authorizer

return &eventHubClient, nil
}

// GetPostgreSQLServerE is a helper function that gets the server.
func GetEventHubNamespaceE(t testing.TestingT, subscriptionID string, resGroupName string, namespace string) (*.Server, error) {
// Create a postgresql Server client
eventHubClient, err := GetEventHubClientE(subscriptionID)
if err != nil {
return nil, err
}

// Get the corresponding server client
postgresqlServer, err := eventHubClient.Get(context.Background(), resGroupName, namespace)
if err != nil {
return nil, err
}

// TODO: temp
require.NoError(t, err)

//Return server
return &postgresqlServer, nil
}
41 changes: 41 additions & 0 deletions modules/azure/eventhub_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//go:build azure
// +build azure

// NOTE: We use build tags to differentiate azure testing because we currently do not have azure access setup for
// CircleCI.

package azure

import (
"testing"

"github.com/stretchr/testify/require"
)

/*
The below tests are currently stubbed out, with the expectation that they will throw errors.
If/when CRUD methods are introduced for Azure PostgreSQL server and database, these tests can be extended
*/

func TestGetEventHubE(t *testing.T) {
t.Parallel()

resGroupName := ""
namespace := ""
subscriptionID := ""

_, err := GetEventHubE(t, subscriptionID, resGroupName, namespace)
require.Error(t, err)
}

// func TestGetPostgreSQLDBE(t *testing.T) {
// t.Parallel()

// resGroupName := ""
// serverName := ""
// subscriptionID := ""
// dbName := ""

// _, err := GetPostgreSQLDBE(t, subscriptionID, resGroupName, serverName, dbName)
// require.Error(t, err)
// }
11 changes: 11 additions & 0 deletions test/azure/terraform_azure_eventhub_example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// TODO: TestTerraformAzureEventhubkExample

package test

import (
"testing"
)

func TestTerraformAzureDiskExample(t *testing.T) {
t.Parallel()
}