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

Initial check in for Icinga2 Provider/Resource #8306

Merged
merged 19 commits into from
Dec 12, 2016

Conversation

lrsmith
Copy link
Contributor

@lrsmith lrsmith commented Aug 19, 2016

Initial check in for review and comment for an Icinga2 Provider. New to GO and test all comments/critiques appreciated.

Need to add one more resource for Icinga2 Service Checks.
Need to address disabling SSL VERIFY option. disabled for now due to self signed cert.

…S Certs will skip verification. Controllable via provider or env variable being set
@jtopjian
Copy link
Contributor

Icinga is something I have on my list of things to explore, so this is interesting.

Ideally, I think all of the Icinga interactions would be wrapped up into a separate Go Icinga client library, so in the Terraform provider, you'd just end up doing things like:

checkResult, err := icinga.CreateCheck(...)

As well, all of the JSON bodies could be wrapped into some Go structs and you could have Go do most of the JSON building for you.

There are a few providers in Terraform that you can use as examples. The first one I found was Grafana. Note how the Grafana interactions are in a separate library and how that library structures the data.

Just some thoughts! I think this would be awesome to have in Terraform and if I had more time, I'd love to jump in and help 😄

@stack72
Copy link
Contributor

stack72 commented Oct 26, 2016

Hi @lrsmith

Any thoughts on how this PR is progressing? Is this something that you are going to continue working on?

Paul

@stack72 stack72 added the waiting-response An issue/pull request is waiting for a response from the community label Oct 26, 2016
@lrsmith
Copy link
Contributor Author

lrsmith commented Oct 26, 2016

@stack72

Paul

I'm refactoring based on some feedback to make it more like the grafana and rundeck providers. So I've been working on https://github.com/lrsmith/go-icinga2-api and I will modify the icinga2 provider to use that.

I am continuing to work on it just got pulled away for a bit. I should be back to it his week and have something for review soon.

@stack72
Copy link
Contributor

stack72 commented Oct 26, 2016

Very nice - look forward to trying it out. Please let me know when you need another review / testing

Paul

@stack72 stack72 removed the waiting-response An issue/pull request is waiting for a response from the community label Oct 26, 2016
@stack72
Copy link
Contributor

stack72 commented Dec 1, 2016

Hi @lrsmith

Just checking back in on this :)

Paul

@lrsmith
Copy link
Contributor Author

lrsmith commented Dec 3, 2016

I am finishing up the services code and once I commit that should be ready for review and comments. I'm shooting for next week to commit the code.

@lrsmith lrsmith changed the title [WIP] Initial check in for Icinga2 Provider/Resource Initial check in for Icinga2 Provider/Resource Dec 6, 2016
@lrsmith
Copy link
Contributor Author

lrsmith commented Dec 6, 2016

Ready for initial review

Copy link
Contributor

@stack72 stack72 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @lrsmith

Thanks for the work here - as you can see, from the first 2 resources, there are similar comments in place. If you can fix those up across the entire set of resources that would be excellent

Also, the PR needs documentation - right now, users won't understand what is required and what is not

Thanks

Paul

Type: schema.TypeBool,
Optional: true,
DefaultFunc: EnvBoolDefaultFunc("ICINGA2_INSECURE_SKIP_TLS_VERIFY", false),
Description: descriptions["insecure"],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't see "insecure" in the descriptions map?

d.Get("insecure").(bool),
)

return config, nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there any conditions we can actually throw an error when creating a new Client?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still need to look at this one.

func init() {
descriptions = map[string]string{
"api_url": "The address of the Icinga2 server.\n",
"api_user": "The user to authenticate to the Iccinga2 Server as.\n",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo in Iccinga

t.Fatal("ICINGA2_API_PASSWORD must be set for acceptance tests")
}

v = os.Getenv("ICINGA2_INSECURE_SKIP_TLS_VERIFY")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this 100% required? It defaults to false right?

Type: schema.TypeString,
Required: true,
ForceNew: true,
//Elem: &schema.Schema{Type: schema.TypeString},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cleanup old comments please

return nil
}

func resourceIcinga2CheckcommandUpdate(d *schema.ResourceData, meta interface{}) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IF we don't need an Update, then we can omit this


name := d.Get("name").(string)

return client.DeleteCheckcommand(name)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this not return any errors if failure?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DeleteCheckCommand will return an error or nil, so I was just passing that back up.


for _, host := range hosts {
if host.Name == hostname {
d.SetId(hostname)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should error out if we don't find our host

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be addressed

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See CheckCommand suggestion on this

}
}

return nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Create -> Read func

return nil
}

func resourceIcinga2HostUpdate(d *schema.ResourceData, meta interface{}) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Omit if not required

@stack72 stack72 added the waiting-response An issue/pull request is waiting for a response from the community label Dec 6, 2016
@lrsmith
Copy link
Contributor Author

lrsmith commented Dec 7, 2016

Went through and made changes on other resource per comments

Copy link
Contributor

@stack72 stack72 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @lrsmith

I have made 1 last comment and then I think we are good to go! I am just going to run the tests against the docker icinga2 container to make sure

Paul


for _, checkcommand := range checkcommands {
if checkcommand.Name == name {
d.SetId(name)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think maybe a better way to handle this would be to do something like this

found := false
for _, checkcommand := range checkcommands {
 		if checkcommand.Name == name {
 			d.SetId(name)
      found = true
    }
}

if !found {
  return fmt.Errorf("Failed to create Checkcommand %s : %s", name, err)
}


for _, host := range hosts {
if host.Name == hostname {
d.SetId(hostname)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See CheckCommand suggestion on this

return err
}

for _, hostgroup := range hostgroups {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See CheckCommand for how I think this should be addressed

return err
}

for _, service := range services {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see CheckCommand for how I think this should be addressed

@stack72
Copy link
Contributor

stack72 commented Dec 8, 2016

Hi @lrsmith

I just ran the tests and got a panic. I set up the tests as follows:

docker run -d -ti --name icinga2-api -p 4080:80 -p 4665:5665 icinga/icinga2
export ICINGA2_API_URL=https://192.168.99.100:4665
export ICINGA2_API_USER=root
export ICINGA2_API_PASSWORD=icinga

Then the command make testacc TEST=./builtin/providers/icinga2 meant this happened:

% make testacc TEST=./builtin/providers/icinga2                                   ✭
==> Checking that code complies with gofmt requirements...
go generate $(go list ./... | grep -v /terraform/vendor/)
2016/12/08 12:59:03 Generated command/internal_plugin_list.go
TF_ACC=1 go test ./builtin/providers/icinga2 -v  -timeout 120m
=== RUN   TestProvider
--- PASS: TestProvider (0.00s)
=== RUN   TestProviderImpl
--- PASS: TestProviderImpl (0.00s)
=== RUN   TestAccCreateCheckcommand
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x48 pc=0x1b1df6]

goroutine 208 [running]:
panic(0x4f2460, 0xc4200101c0)
	/usr/local/Cellar/go/1.7.3/libexec/src/runtime/panic.go:500 +0x1a1
github.com/hashicorp/terraform/vendor/github.com/lrsmith/go-icinga2-api/iapi.(*Server).NewAPIRequest(0xc4201de900, 0x582997, 0x3, 0xc4201a1800, 0x34, 0xc4202c88c0, 0xc3, 0x126, 0x0, 0x0, ...)
	/Users/stacko/Code/go/src/github.com/hashicorp/terraform/vendor/github.com/lrsmith/go-icinga2-api/iapi/client.go:99 +0x3e6
github.com/hashicorp/terraform/vendor/github.com/lrsmith/go-icinga2-api/iapi.(*Server).CreateCheckcommand(0xc4201de900, 0xc4200f8b21, 0x1d, 0xc4200f8b61, 0x1c, 0xc4202b3260, 0xc4201fd000, 0xc4201fd000, 0xc420186820, 0xdbdd1, ...)
	/Users/stacko/Code/go/src/github.com/hashicorp/terraform/vendor/github.com/lrsmith/go-icinga2-api/iapi/checkcommands.go:55 +0x29a
github.com/hashicorp/terraform/builtin/providers/icinga2.resourceIcinga2CheckcommandCreate(0xc42019cf00, 0x561200, 0xc4201de900, 0x800, 0xd394)
	/Users/stacko/Code/go/src/github.com/hashicorp/terraform/builtin/providers/icinga2/resource_icinga2_checkcommand.go:56 +0x32c
github.com/hashicorp/terraform/helper/schema.(*Resource).Apply(0xc420018de0, 0xc4201aa870, 0xc42019e640, 0x561200, 0xc4201de900, 0xc4201a1701, 0x3a, 0x0)
	/Users/stacko/Code/go/src/github.com/hashicorp/terraform/helper/schema/resource.go:162 +0x30e
github.com/hashicorp/terraform/helper/schema.(*Provider).Apply(0xc420018ea0, 0xc420196460, 0xc4201aa870, 0xc42019e640, 0x1, 0xc420243708, 0x9b702)
	/Users/stacko/Code/go/src/github.com/hashicorp/terraform/helper/schema/provider.go:212 +0x9b
github.com/hashicorp/terraform/terraform.(*EvalApply).Eval(0xc4201df540, 0x7d8580, 0xc42024b520, 0x2, 0x2, 0x58302d, 0x4)
	/Users/stacko/Code/go/src/github.com/hashicorp/terraform/terraform/eval_apply.go:67 +0x34b
github.com/hashicorp/terraform/terraform.EvalRaw(0x7cd8a0, 0xc4201df540, 0x7d8580, 0xc42024b520, 0x541d60, 0x0, 0x0, 0x0)
	/Users/stacko/Code/go/src/github.com/hashicorp/terraform/terraform/eval.go:53 +0x17f
github.com/hashicorp/terraform/terraform.(*EvalSequence).Eval(0xc420292b80, 0x7d8580, 0xc42024b520, 0x2, 0x2, 0x58302d, 0x4)
	/Users/stacko/Code/go/src/github.com/hashicorp/terraform/terraform/eval_sequence.go:10 +0x6c
github.com/hashicorp/terraform/terraform.EvalRaw(0x7ce260, 0xc420292b80, 0x7d8580, 0xc42024b520, 0x7ce260, 0xc420292b80, 0xc420292ca0, 0x1a)
	/Users/stacko/Code/go/src/github.com/hashicorp/terraform/terraform/eval.go:53 +0x17f
github.com/hashicorp/terraform/terraform.Eval(0x7ce260, 0xc420292b80, 0x7d8580, 0xc42024b520, 0xc420292b80, 0x7ce260, 0xc420292b80, 0x0)
	/Users/stacko/Code/go/src/github.com/hashicorp/terraform/terraform/eval.go:34 +0x53
github.com/hashicorp/terraform/terraform.(*Graph).walk.func1(0x563e00, 0xc42002d998, 0x0, 0x0)
	/Users/stacko/Code/go/src/github.com/hashicorp/terraform/terraform/graph.go:301 +0xe7c
github.com/hashicorp/terraform/dag.(*AcyclicGraph).Walk.func3(0xc420265400, 0xc42023abe0, 0xc420265410, 0xc4202555f0, 0xc420265420, 0x563e00, 0xc42002d998, 0xc420239e00, 0xc420239f20)
	/Users/stacko/Code/go/src/github.com/hashicorp/terraform/dag/dag.go:246 +0x251
created by github.com/hashicorp/terraform/dag.(*AcyclicGraph).Walk
	/Users/stacko/Code/go/src/github.com/hashicorp/terraform/dag/dag.go:255 +0x6a6
exit status 2
FAIL	github.com/hashicorp/terraform/builtin/providers/icinga2	0.113s
make: *** [testacc] Error 1

@lrsmith
Copy link
Contributor Author

lrsmith commented Dec 8, 2016

Paul, thanks I will take a look.

I definitely need to put some more code around catching this and I'll add that shortly. The API_URL is missing a bit, it needs "/v1" on the end. My original intent was to require it on that line since it looks like in was there to allow for v2 of the api etc. Thinking about it now, I will look at making the a variable that can be over-written and will default to v1. I'll also work on the changes above. Update PR coming soon.

I pulled down the docker container and it worked with it set to
export ICINGA2_API_URL=https://127.0.0.1:4665/v1

Looks like the 'default' hosts in the docker image are different then the vagrant too, so some of the acceptance tests will fail. I'll use the docker container and adjust and add the in too.

Thanks
Len

@lrsmith
Copy link
Contributor Author

lrsmith commented Dec 10, 2016

Paul,
I added code to verify the URL and handle the case where the server is unavailable. I also modified the code to test if the resource was found when iterating through the list. I also added a timeout and test cases in the underlying go-icinga2-api code.

Set a bad port on server, via ENV variable

--- FAIL: TestAccCreateCheckcommand (0.00s)
testing.go:265: Step 0 error: Error refreshing: 1 error(s) occurred:

	* Get https://127.0.0.1:4666/v1: dial tcp 127.0.0.1:4666: getsockopt: connection refused

Forget to add /v1 or invalid URL path to ENV variable

--- FAIL: TestAccCreateCheckcommand (0.00s)
testing.go:265: Step 0 error: Error refreshing: 1 error(s) occurred:

	* Error : Invalid API version  specified. Only v1 is currently supported.

or
* Error : Invalid API version /v2 specified. Only v1 is currently supported.

Server is down

--- FAIL: TestAccCreateCheckcommand (60.01s)
testing.go:265: Step 0 error: Error refreshing: 1 error(s) occurred:

	* Get https://127.0.0.2:4665/v1: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)

@stack72
Copy link
Contributor

stack72 commented Dec 12, 2016

Hi @lrsmith

Thanks for making the changes here - do you know of a docker container I can use to test this now? When i run it against the official container, I get the following:

% make testacc TEST=./builtin/providers/icinga2
==> Checking that code complies with gofmt requirements...
go generate $(go list ./... | grep -v /terraform/vendor/)
2016/12/12 15:19:44 Generated command/internal_plugin_list.go
TF_ACC=1 go test ./builtin/providers/icinga2 -v  -timeout 120m
=== RUN   TestProvider
--- PASS: TestProvider (0.00s)
=== RUN   TestProviderImpl
--- PASS: TestProviderImpl (0.00s)
=== RUN   TestAccCreateCheckcommand
--- FAIL: TestAccCreateCheckcommand (0.02s)
	testing.go:265: Step 0 error: Error refreshing: 1 error(s) occurred:

		* Error : Invalid API version  specified. Only v1 is currently supported.
=== RUN   TestAccCreateBasicHost
--- FAIL: TestAccCreateBasicHost (0.02s)
	testing.go:265: Step 0 error: Error refreshing: 1 error(s) occurred:

		* Error : Invalid API version  specified. Only v1 is currently supported.
=== RUN   TestAccCreateVariableHost
--- FAIL: TestAccCreateVariableHost (0.02s)
	testing.go:265: Step 0 error: Error refreshing: 1 error(s) occurred:

		* Error : Invalid API version  specified. Only v1 is currently supported.
=== RUN   TestAccCreateBasicHostGroup
--- FAIL: TestAccCreateBasicHostGroup (0.02s)
	testing.go:265: Step 0 error: Error refreshing: 1 error(s) occurred:

		* Error : Invalid API version  specified. Only v1 is currently supported.
=== RUN   TestAccCreateService
--- FAIL: TestAccCreateService (0.02s)
	testing.go:265: Step 0 error: Error refreshing: 1 error(s) occurred:

		* Error : Invalid API version  specified. Only v1 is currently supported.
FAIL
exit status 1
FAIL	github.com/hashicorp/terraform/builtin/providers/icinga2	0.108s
make: *** [testacc] Error 1

@stack72
Copy link
Contributor

stack72 commented Dec 12, 2016

Ignore that comment - I should have added /v1 to the end of the URL!

This LGTM now :)

% make testacc TEST=./builtin/providers/icinga2
==> Checking that code complies with gofmt requirements...
go generate $(go list ./... | grep -v /terraform/vendor/)
2016/12/12 15:27:32 Generated command/internal_plugin_list.go
TF_ACC=1 go test ./builtin/providers/icinga2 -v  -timeout 120m
=== RUN   TestProvider
--- PASS: TestProvider (0.00s)
=== RUN   TestProviderImpl
--- PASS: TestProviderImpl (0.00s)
=== RUN   TestAccCreateCheckcommand
--- PASS: TestAccCreateCheckcommand (0.19s)
=== RUN   TestAccCreateBasicHost
--- PASS: TestAccCreateBasicHost (0.42s)
=== RUN   TestAccCreateVariableHost
--- PASS: TestAccCreateVariableHost (0.46s)
=== RUN   TestAccCreateBasicHostGroup
--- PASS: TestAccCreateBasicHostGroup (0.25s)
=== RUN   TestAccCreateService
--- PASS: TestAccCreateService (0.43s)
PASS
ok  	github.com/hashicorp/terraform/builtin/providers/icinga2	1.760s

Thanks for all the work here

Paul

@stack72 stack72 merged commit 015e96d into hashicorp:master Dec 12, 2016
@jtopjian
Copy link
Contributor

Cool!

@lrsmith very nice work!

@ghost
Copy link

ghost commented Apr 18, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@ghost ghost locked and limited conversation to collaborators Apr 18, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement new-provider waiting-response An issue/pull request is waiting for a response from the community
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants