-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathCreateLoadbalancerRuleSetExample.cs
160 lines (137 loc) · 6.12 KB
/
CreateLoadbalancerRuleSetExample.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
* Copyright (c) 2020, 2025, Oracle and/or its affiliates. All rights reserved.
* This software is dual-licensed to you under the Universal Permissive License (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl or Apache License 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose either license.
*/
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Oci.LoadbalancerService;
using Oci.LoadbalancerService.Models;
using Oci.LoadbalancerService.Requests;
using Oci.Common.Auth;
using Oci.Common.Waiters;
namespace Oci.Examples
{
/**
* Please refer to https://docs.cloud.oracle.com/iaas/api/#/en/loadbalancer/latest/RuleSet/CreateRuleSet
* for information about using the API to create a rule set.
* This example requires an existing LoadBalancer and it will do the following
* 1) Create new rule set.
* 2) Create a new listener with a reference to the new rule set.
*/
public class LoadbalancerExample
{
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
private static string RuleSetName = "oci_dotnet_sdk_example_ruleset";
private static int ListenerPort = 8080;
private static string ListenerName = "oci_dotnet_sdk_example_listener";
private static string ListenerProtocol = "HTTP";
public static async Task MainRuleSet()
{
logger.Info("Starting example");
var provider = new ConfigFileAuthenticationDetailsProvider("DEFAULT");
var compartmentId = Environment.GetEnvironmentVariable("OCI_COMPARTMENT_ID");
var loadBalancerId = Environment.GetEnvironmentVariable("LOADBALANCER_ID");
var backendSetName = Environment.GetEnvironmentVariable("BACKEND_SET_NAME");
var loadBalancerClient = new LoadBalancerClient(provider);
try
{
var ruleSet = await CreateRuleSet(loadBalancerClient, loadBalancerId);
await CreateListener(loadBalancerClient, loadBalancerId, ruleSet, backendSetName);
}
catch (Exception e)
{
logger.Error($"Failed to add ruleset: {e}");
}
finally
{
loadBalancerClient.Dispose();
}
logger.Info("End example");
}
private static async Task<RuleSet> CreateRuleSet(LoadBalancerClient lbClient, string loadBalancerId)
{
logger.Info($"Creating new rule set: {RuleSetName}");
List<Rule> rules = new List<Rule>();
var addHttpRequestHeaderRule = new AddHttpRequestHeaderRule
{
Header = "someRequestHeader",
Value = "/example/1"
};
var extendHttpRequestHeaderValueRule = new ExtendHttpRequestHeaderValueRule
{
Header = "someRequestHeader",
Suffix = "Some-static-suffix-value",
Prefix = "Some-static-prefix-value"
};
var removeHttpResponseHeaderRule = new RemoveHttpResponseHeaderRule
{
Header = "someResponseHeader"
};
rules.Add(addHttpRequestHeaderRule);
rules.Add(extendHttpRequestHeaderValueRule);
rules.Add(removeHttpResponseHeaderRule);
var createRuleSetDetails = new CreateRuleSetDetails
{
Name = RuleSetName,
Items = rules
};
var createRuleSetRequest = new CreateRuleSetRequest
{
CreateRuleSetDetails = createRuleSetDetails,
LoadBalancerId = loadBalancerId
};
var createRuleSetResponse = await lbClient.CreateRuleSet(createRuleSetRequest);
var waiterConfiguration = new WaiterConfiguration
{
MaxAttempts = 20,
GetNextDelayInSeconds = DelayStrategy.GetExponentialDelayInSeconds
};
var getWorkRequestRequest = new GetWorkRequestRequest
{
WorkRequestId = createRuleSetResponse.OpcWorkRequestId
};
lbClient.Waiters.ForWorkRequest(getWorkRequestRequest, waiterConfiguration, WorkRequest.LifecycleStateEnum.Succeeded).Execute();
logger.Info($"RuleSet: {RuleSetName} is created");
var getRuleSetRequest = new GetRuleSetRequest
{
LoadBalancerId = loadBalancerId,
RuleSetName = RuleSetName
};
var getRuleSetResponse = await lbClient.GetRuleSet(getRuleSetRequest);
return getRuleSetResponse.RuleSet;
}
private static async Task CreateListener(LoadBalancerClient lbClient, string loadBalancerId, RuleSet ruleSet, string backendSetName)
{
logger.Info($"Creating new listener: {ListenerName} with ruleset: {RuleSetName}");
var ruleSetNames = new List<string>(){
ruleSet.Name
};
var createListenerDetails = new CreateListenerDetails
{
Name = ListenerName,
DefaultBackendSetName = backendSetName,
Port = ListenerPort,
Protocol = ListenerProtocol,
RuleSetNames = ruleSetNames
};
var createListenerRequest = new CreateListenerRequest
{
CreateListenerDetails = createListenerDetails,
LoadBalancerId = loadBalancerId
};
var response = await lbClient.CreateListener(createListenerRequest);
var waiterConfiguration = new WaiterConfiguration
{
MaxAttempts = 20,
GetNextDelayInSeconds = DelayStrategy.GetExponentialDelayInSeconds
};
var getWorkRequestRequest = new GetWorkRequestRequest
{
WorkRequestId = response.OpcWorkRequestId
};
lbClient.Waiters.ForWorkRequest(getWorkRequestRequest, waiterConfiguration, WorkRequest.LifecycleStateEnum.Succeeded).Execute();
logger.Info($"listener {ListenerName} is created");
}
}
}