Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
pablo-ruth committed Nov 29, 2019
0 parents commit 1ad96e4
Show file tree
Hide file tree
Showing 8 changed files with 679 additions and 0 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: PR
on: [pull_request]
jobs:
build:
name: Build
runs-on: ubuntu-latest
steps:
- name: Set up Go 1.13
uses: actions/setup-go@v1
with:
go-version: 1.13

- name: Check out code into the Go module directory
uses: actions/checkout@v1

- name: Build
run: go build -o terraform-provider-ldap_${GITHUB_REF}

- name: Setup Lint
run: curl -LO https://github.com/golangci/golangci-lint/releases/download/v1.19.1/golangci-lint-1.19.1-linux-amd64.tar.gz && tar -xf golangci-lint-1.19.1-linux-amd64.tar.gz

- name: Lint
run: golangci-lint-1.19.1-linux-amd64/golangci-lint run
41 changes: 41 additions & 0 deletions .github/workflows/tag.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Tag
on:
push:
tags:
- "v*.*.*"

jobs:
build:
name: Build
runs-on: ubuntu-latest
steps:
- name: Set up Go 1.13
uses: actions/setup-go@v1
with:
go-version: 1.13
id: go

- name: Get the version
id: get_version
run: echo ::set-output name=VERSION::${GITHUB_REF/refs\/tags\//}

- name: Check out code into the Go module directory
uses: actions/checkout@v1

- name: Build
run: go build -o terraform-provider-ldap_${{ steps.get_version.outputs.VERSION }}

- name: Setup Lint
run: curl -LO https://github.com/golangci/golangci-lint/releases/download/v1.19.1/golangci-lint-1.19.1-linux-amd64.tar.gz && tar -xf golangci-lint-1.19.1-linux-amd64.tar.gz

- name: Lint
run: golangci-lint-1.19.1-linux-amd64/golangci-lint run

- name: Upload binaries to release
uses: svenstaro/upload-release-action@v1-release
with:
repo_token: ${{ secrets.GITHUB_TOKEN }}
file: terraform-provider-ldap_${{ steps.get_version.outputs.VERSION }}
asset_name: terraform-provider-ldap_${{ steps.get_version.outputs.VERSION }}
tag: ${{ github.ref }}
overwrite: true
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
build:
go build -o terraform-provider-ldap_v0.0.1
9 changes: 9 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module github.com/Ouest-France/terraform-provider-ldap

go 1.13

require (
github.com/Ouest-France/goldap v0.1.0
github.com/go-ldap/ldap/v3 v3.1.3
github.com/hashicorp/terraform v0.12.16
)
433 changes: 433 additions & 0 deletions go.sum

Large diffs are not rendered by default.

61 changes: 61 additions & 0 deletions ldap/provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package ldap

import (
"github.com/Ouest-France/goldap"
"github.com/hashicorp/terraform/helper/schema"
)

func Provider() *schema.Provider {
return &schema.Provider{
Schema: map[string]*schema.Schema{
"host": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "LDAP host",
},
"port": &schema.Schema{
Type: schema.TypeInt,
Required: true,
Description: "LDAP port",
},
"bind_user": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "FortiADC username",
},
"bind_password": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "FortiADC password",
},
"tls": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Disable TLS Verify",
},
},
ResourcesMap: map[string]*schema.Resource{
"ldap_group": resourceLDAPGroup(),
},
ConfigureFunc: providerConfigure,
}
}

func providerConfigure(d *schema.ResourceData) (interface{}, error) {

client := &goldap.Client{
Host: d.Get("host").(string),
Port: d.Get("port").(int),
BindUser: d.Get("bind_user").(string),
BindPassword: d.Get("bind_password").(string),
TLS: d.Get("tls").(bool),
}

err := client.Connect()
if err != nil {
return nil, err
}

return client, nil
}
94 changes: 94 additions & 0 deletions ldap/resource_ldap_group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package ldap

import (
"fmt"

"github.com/Ouest-France/goldap"
"github.com/go-ldap/ldap/v3"
"github.com/hashicorp/terraform/helper/schema"
)

func resourceLDAPGroup() *schema.Resource {
return &schema.Resource{
Create: resourceLDAPGroupCreate,
Read: resourceLDAPGroupRead,
Delete: resourceLDAPGroupDelete,

Schema: map[string]*schema.Schema{
"ou": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"members": &schema.Schema{
Type: schema.TypeSet,
Optional: true,
ForceNew: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
}
}

func resourceLDAPGroupCreate(d *schema.ResourceData, m interface{}) error {
client := m.(*goldap.Client)

dn := fmt.Sprintf("CN=%s,%s", d.Get("name").(string), d.Get("ou").(string))

members := []string{}
memberSet := d.Get("members").(*schema.Set)
for _, member := range memberSet.List() {
members = append(members, member.(string))
}

err := client.CreateGroup(dn, d.Get("name").(string), members)
if err != nil {
return err
}

d.SetId(dn)

return resourceLDAPGroupRead(d, m)
}

func resourceLDAPGroupRead(d *schema.ResourceData, m interface{}) error {
client := m.(*goldap.Client)

dn := fmt.Sprintf("CN=%s,%s", d.Get("name").(string), d.Get("ou").(string))

attributes, err := client.ReadGroup(dn)
if err != nil {
if err.(*ldap.Error).ResultCode == 32 {
// Object doesn't exist
d.SetId("")
}
}

members := []string{}
for name, values := range attributes {

if name == "member" && len(values) >= 1 {
members = append(members, values...)
}
}
err = d.Set("members", members)

return err
}

func resourceLDAPGroupDelete(d *schema.ResourceData, m interface{}) error {
client := m.(*goldap.Client)

dn := fmt.Sprintf("CN=%s,%s", d.Get("name").(string), d.Get("ou").(string))

err := client.DeleteGroup(dn)

return err
}
16 changes: 16 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package main

import (
"github.com/Ouest-France/terraform-provider-ldap/ldap"

"github.com/hashicorp/terraform/plugin"
"github.com/hashicorp/terraform/terraform"
)

func main() {
plugin.Serve(&plugin.ServeOpts{
ProviderFunc: func() terraform.ResourceProvider {
return ldap.Provider()
},
})
}

0 comments on commit 1ad96e4

Please sign in to comment.