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

[Ready for Review] Added Redis cache samples #1098

Merged
merged 3 commits into from
Sep 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,6 @@ public class RedisUpdateParametersInner {
@JsonProperty(value = "properties.sku")
private Sku sku;

/**
* Resource location.
*/
@JsonProperty(value = "properties.location")
private String location;

/**
* Resource tags.
*/
Expand Down Expand Up @@ -218,26 +212,6 @@ public RedisUpdateParametersInner withSku(Sku sku) {
return this;
}

/**
* Get the location value.
*
* @return the location value
*/
public String location() {
return this.location;
}

/**
* Set the location value.
*
* @param location the location value to set
* @return the RedisUpdateParametersInner object itself.
*/
public RedisUpdateParametersInner withLocation(String location) {
this.location = location;
return this;
}

/**
* Get the tags value.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
// license information.

/**
* This package contains the classes for compute samples.
* This package contains the classes for network samples.
*/
package com.microsoft.azure.management.network.samples;
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
/**
*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*
*/

package com.microsoft.azure.management.rediscache.samples;

import com.microsoft.azure.Azure;
import com.microsoft.azure.management.redis.DayOfWeek;
import com.microsoft.azure.management.redis.RebootType;
import com.microsoft.azure.management.redis.RedisAccessKeys;
import com.microsoft.azure.management.redis.RedisCache;
import com.microsoft.azure.management.redis.RedisCachePremium;
import com.microsoft.azure.management.redis.RedisCaches;
import com.microsoft.azure.management.redis.RedisKeyType;
import com.microsoft.azure.management.resources.fluentcore.arm.Region;
import com.microsoft.azure.management.samples.Utils;
import okhttp3.logging.HttpLoggingInterceptor;

import java.io.File;
import java.util.List;

/**
* Azure Redis sample for managing Redis Cache:
* - Create a Redis Cache and print out hostname.
* - Get access keys.
* - Regenerate access keys.
* - Create another 2 Redis Caches with Premium Sku.
* - List all Redis Caches in a resource group – for each cache with Premium Sku:
* - set Redis patch schedule to Monday at 5 am.
* - update shard count.
* - enable non-SSL port.
* - modify max memory policy and reserved settings.
* - restart it.
* - Clean up all resources.
*/

public final class ManageRedisCache {

/**
* Main entry point.
* @param args the parameters
*/
public static void main(String[] args) {

final String redisCacheName1 = Utils.createRandomName("rc1");
final String redisCacheName2 = Utils.createRandomName("rc2");
final String redisCacheName3 = Utils.createRandomName("rc3");
final String rgName = Utils.createRandomName("rgRCMC");

try {

final File credFile = new File(System.getenv("AZURE_AUTH_LOCATION"));

Azure azure = Azure
.configure()
.withLogLevel(HttpLoggingInterceptor.Level.BASIC)
.authenticate(credFile)
.withDefaultSubscription();

// Print selected subscription
System.out.println("Selected subscription: " + azure.subscriptionId());

try {
// ============================================================
// Create a redis cache

System.out.println("Creating a Redis Cache");

RedisCache redisCache1 = azure.redisCaches().define(redisCacheName1)
.withRegion(Region.US_CENTRAL)
.withNewResourceGroup(rgName)
.withBasicSku()
.create();

System.out.println("Created a Redis Cache:");
Utils.print(redisCache1);

// ============================================================
// Get | regenerate Redis Cache access keys

System.out.println("Getting Redis Cache access keys");
RedisAccessKeys redisAccessKeys = redisCache1.keys();
Utils.print(redisAccessKeys);

System.out.println("Regenerating secondary Redis Cache access key");
redisAccessKeys = redisCache1.regenerateKey(RedisKeyType.SECONDARY);
Utils.print(redisAccessKeys);

// ============================================================
// Create another two Redis Caches

System.out.println("Creating two more Redis Caches with Premium Sku");

RedisCache redisCache2 = azure.redisCaches().define(redisCacheName2)
.withRegion(Region.US_CENTRAL)
.withNewResourceGroup(rgName)
.withPremiumSku()
.create();

System.out.println("Created a Redis Cache:");
Utils.print(redisCache2);

RedisCache redisCache3 = azure.redisCaches().define(redisCacheName3)
.withRegion(Region.US_CENTRAL)
.withNewResourceGroup(rgName)
.withPremiumSku(2)
.create();

System.out.println("Created a Redis Cache:");
Utils.print(redisCache3);

// ============================================================
// List Redis Caches inside the resource group

Copy link
Contributor

Choose a reason for hiding this comment

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

Suggest that you update this comment with what is happening with Premium SKU inside the list walk

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed.

System.out.println("Listing Redis Caches");

RedisCaches redisCaches = azure.redisCaches();

List<RedisCache> caches = redisCaches.listByGroup(rgName);

// Walk through all the caches
for (RedisCache redis : caches) {
// If the instance of the Redis Cache is Premium Sku
if (redis.isPremium()) {
RedisCachePremium premium = redis.asPremium();

// Update each Premium Sku Redis Cache instance
System.out.println("Updating Premium Redis Cache");
premium.update()
.withPatchSchedule(DayOfWeek.MONDAY, 5)
.withShardCount(4)
.withNonSslPort()
.withRedisConfiguration("maxmemory-policy", "allkeys-random")
.withRedisConfiguration("maxmemory-reserved", "20")
.apply();

System.out.println("Updated Redis Cache:");
Utils.print(premium);

// Restart Redis Cache
System.out.println("Restarting updated Redis Cache");
premium.forceReboot(RebootType.ALL_NODES);

System.out.println("Redis Cache restart scheduled");
}
}

// ============================================================
// Delete a Redis Cache

System.out.println("Deleting a Redis Cache - " + redisCache1.name());

azure.redisCaches().delete(redisCache1.id());

System.out.println("Deleted Redis Cache");
} catch (Exception f) {
System.out.println(f.getMessage());
f.printStackTrace();
} finally {
if (azure.resourceGroups().getByName(rgName) != null) {
System.out.println("Deleting Resource Group: " + rgName);
azure.resourceGroups().delete(rgName);
System.out.println("Deleted Resource Group: " + rgName);
} else {
System.out.println("Did not create any resources in Azure. No clean up is necessary");
}
}
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}

private ManageRedisCache() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for
// license information.

/**
* This package contains the classes for Redis cache samples.
*/
package com.microsoft.azure.management.rediscache.samples;
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
// license information.

/**
* This package contains the classes for compute samples.
* This package contains the classes for resource samples.
*/
package com.microsoft.azure.management.resources.samples;
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
import com.microsoft.azure.management.network.NetworkSecurityRule;
import com.microsoft.azure.management.network.PublicIpAddress;
import com.microsoft.azure.management.network.Subnet;
import com.microsoft.azure.management.redis.RedisAccessKeys;
import com.microsoft.azure.management.redis.RedisCache;
import com.microsoft.azure.management.redis.RedisCachePremium;
import com.microsoft.azure.management.redis.ScheduleEntry;
import com.microsoft.azure.management.storage.StorageAccount;
import com.microsoft.azure.management.storage.StorageAccountKey;

Expand Down Expand Up @@ -316,6 +320,57 @@ public static void print(List<StorageAccountKey> storageAccountKeys) {
}
}

/**
* Print Redis Cache.
* @param redisCache a Redis cache.
*/
public static void print(RedisCache redisCache) {
StringBuilder redisInfo = new StringBuilder()
.append("Redis Cache Name: ").append(redisCache.name())
.append("\n\tResource group: ").append(redisCache.resourceGroupName())
.append("\n\tRegion: ").append(redisCache.region())
.append("\n\tSKU Name: ").append(redisCache.sku().name())
.append("\n\tSKU Family: ").append(redisCache.sku().family())
.append("\n\tHost name: ").append(redisCache.hostName())
.append("\n\tSSL port: ").append(redisCache.sslPort())
.append("\n\tNon-SSL port (6379) enabled: ").append(redisCache.nonSslPort());
if (redisCache.redisConfiguration() != null && !redisCache.redisConfiguration().isEmpty()) {
redisInfo.append("\n\tRedis Configuration:");
for (Map.Entry<String, String> redisConfiguration : redisCache.redisConfiguration().entrySet()) {
redisInfo.append("\n\t '").append(redisConfiguration.getKey())
.append("' : '").append(redisConfiguration.getValue()).append("'");
}
}
if (redisCache.isPremium()) {
RedisCachePremium premium = redisCache.asPremium();
List<ScheduleEntry> scheduleEntries = premium.getPatchSchedules();
if (scheduleEntries != null && !scheduleEntries.isEmpty()) {
redisInfo.append("\n\tRedis Patch Schedule:");
for (ScheduleEntry schedule : scheduleEntries) {
redisInfo.append("\n\t\tDay: '").append(schedule.dayOfWeek())
.append("', start at: '").append(schedule.startHourUtc())
.append("', maintenance window: '").append(schedule.maintenanceWindow())
.append("'");
}
}
}

System.out.println(redisInfo.toString());
}

/**
* Print Redis Cache access keys.
* @param redisAccessKeys a keys for Redis Cache
*/
public static void print(RedisAccessKeys redisAccessKeys) {
StringBuilder redisKeys = new StringBuilder()
.append("Redis Access Keys: ")
.append("\n\tPrimary Key: '").append(redisAccessKeys.primaryKey()).append("', ")
.append("\n\tSecondary Key: '").append(redisAccessKeys.secondaryKey()).append("', ");

System.out.println(redisKeys.toString());
}

/**
* Creates and returns a randomized name based on the prefix file for use by the sample.
* @param namePrefix The prefix string to be used in generating the name.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
// license information.

/**
* This package contains the classes for compute samples.
* This package contains the classes for storage samples.
*/
package com.microsoft.azure.management.storage.samples;