1+ package main
2+
3+ import (
4+ "fmt"
5+ "os"
6+
7+ "github.com/onelogin/onelogin-go-sdk/v4/pkg/onelogin"
8+ )
9+
10+ func main () {
11+ // Get credentials from environment variables
12+ clientID := os .Getenv ("ONELOGIN_CLIENT_ID" )
13+ clientSecret := os .Getenv ("ONELOGIN_CLIENT_SECRET" )
14+ subdomain := os .Getenv ("ONELOGIN_SUBDOMAIN" )
15+
16+ if clientID == "" || clientSecret == "" || subdomain == "" {
17+ fmt .Println ("Please set ONELOGIN_CLIENT_ID, ONELOGIN_CLIENT_SECRET, and ONELOGIN_SUBDOMAIN environment variables" )
18+ os .Exit (1 )
19+ }
20+
21+ // Set environment variables for SDK initialization
22+ os .Setenv ("ONELOGIN_CLIENT_ID" , clientID )
23+ os .Setenv ("ONELOGIN_CLIENT_SECRET" , clientSecret )
24+ os .Setenv ("ONELOGIN_SUBDOMAIN" , subdomain )
25+
26+ // Initialize the SDK
27+ sdk , err := onelogin .NewOneloginSDK ()
28+ if err != nil {
29+ fmt .Printf ("Error initializing SDK: %v\n " , err )
30+ os .Exit (1 )
31+ }
32+
33+ // Example 1: Create a custom attribute using the new helper function
34+ fmt .Println ("Example 1: Creating a custom attribute" )
35+
36+ // Create with the new helper that properly structures the payload
37+ createResp , err := sdk .CreateCustomAttribute ("Employee ID" , "employee_id" )
38+ if err != nil {
39+ fmt .Printf ("Error creating custom attribute: %v\n " , err )
40+ os .Exit (1 )
41+ }
42+ fmt .Printf ("Custom attribute created successfully: %+v\n " , createResp )
43+
44+ // Extract the ID from the response for later use
45+ attr := createResp .(map [string ]interface {})
46+ attrID := int (attr ["id" ].(float64 ))
47+ fmt .Printf ("Created attribute ID: %d\n " , attrID )
48+
49+ // Example 2: Get all custom attributes
50+ fmt .Println ("\n Example 2: Get custom attributes" )
51+ customAttrs , err := sdk .GetCustomAttributes ()
52+ if err != nil {
53+ fmt .Printf ("Error getting custom attributes: %v\n " , err )
54+ } else {
55+ fmt .Printf ("Custom attributes: %+v\n " , customAttrs )
56+ }
57+
58+ // Example 3: Get custom attribute by ID
59+ fmt .Println ("\n Example 3: Get custom attribute by ID" )
60+ attrByID , err := sdk .GetCustomAttributeByID (attrID )
61+ if err != nil {
62+ fmt .Printf ("Error getting custom attribute by ID: %v\n " , err )
63+ } else {
64+ fmt .Printf ("Custom attribute by ID: %+v\n " , attrByID )
65+ }
66+
67+ // Example 4: Update the custom attribute
68+ fmt .Println ("\n Example 4: Update custom attribute" )
69+ updateResp , err := sdk .UpdateCustomAttribute (attrID , "Employee Number" , "employee_id" )
70+ if err != nil {
71+ fmt .Printf ("Error updating custom attribute: %v\n " , err )
72+ } else {
73+ fmt .Printf ("Custom attribute updated successfully: %+v\n " , updateResp )
74+ }
75+
76+ // Verify the update
77+ updatedAttr , err := sdk .GetCustomAttributeByID (attrID )
78+ if err != nil {
79+ fmt .Printf ("Error getting updated custom attribute: %v\n " , err )
80+ } else {
81+ fmt .Printf ("Updated custom attribute: %+v\n " , updatedAttr )
82+ }
83+
84+ // Example 5: Delete the custom attribute
85+ fmt .Println ("\n Example 5: Delete custom attribute" )
86+ deleteResp , err := sdk .DeleteCustomAttributes (attrID )
87+ if err != nil {
88+ fmt .Printf ("Error deleting custom attribute: %v\n " , err )
89+ } else {
90+ fmt .Printf ("Custom attribute deleted successfully: %+v\n " , deleteResp )
91+ }
92+
93+ // Verify the deletion
94+ fmt .Println ("\n Verifying deletion - get all custom attributes" )
95+ remainingAttrs , err := sdk .GetCustomAttributes ()
96+ if err != nil {
97+ fmt .Printf ("Error getting remaining custom attributes: %v\n " , err )
98+ } else {
99+ fmt .Printf ("Remaining custom attributes: %+v\n " , remainingAttrs )
100+
101+ // Check if our deleted attribute is still in the list
102+ deleted := true
103+ attrs := remainingAttrs .([]interface {})
104+ for _ , a := range attrs {
105+ attr , ok := a .(map [string ]interface {})
106+ if ! ok {
107+ continue
108+ }
109+
110+ if id , exists := attr ["id" ]; exists && int (id .(float64 )) == attrID {
111+ deleted = false
112+ break
113+ }
114+ }
115+
116+ if deleted {
117+ fmt .Println ("✅ Attribute was successfully deleted!" )
118+ } else {
119+ fmt .Println ("❌ Attribute was NOT successfully deleted!" )
120+ }
121+ }
122+
123+ fmt .Println ("\n Examples completed" )
124+ }
0 commit comments